AWK 不打印输出文件分隔符 OFS

AWK not printing output file separator OFS

提问人:jjk 提问时间:1/23/2018 最后编辑:jjk 更新时间:7/9/2022 访问量:6175

问:

输入

15.01.2018;Payment sent;;500.00;;
20.12.2017;Payment received;10.40;;;

预期输出

15.01.2018;Payment sent;-500.00
20.12.2017;Payment received;10.40

电流输出

15.01.2018Payment sent-500.00
20.12.2017Payment received10.40

有人看到我的命令中的问题了吗?

awk 'BEGIN{OFS=";";FS=";"} {print match($4, /[^ ]/) ? $1$2$3"-"$4 : $1$2$3}' < in.csv > out.csv

谢谢

砰的 一声

评论

0赞 jjk 1/23/2018
编辑时我无法添加简单的“嗨”......所以我在这里做:嗨
0赞 RavinderSingh13 1/23/2018
获得预期产出的条件是什么?既然第一行有 a 而第二行没有?请更具体地说。-
0赞 PesaThe 1/23/2018
您可以使用 : .OFSprint match ... ? $1 OFS $2 ...
0赞 jjk 1/23/2018
@RavinderSingh13 我添加了输入以澄清:如果 $4 包含值,则在 $4 中添加连字符
0赞 jjk 1/23/2018
谢谢你@PesaThe。你能详细说明一下吗?我不明白

答:

1赞 RavinderSingh13 1/23/2018 #1

以下可能会对您有所帮助。awk

awk -F";" '$4~/[0-9]/{$4="-"$4}{gsub(/;+/,";");sub(/;$/,"")}  1' OFS=";"  Input_file

输出如下。

15.01.2018;Payment sent;-500.00
20.12.2017;Payment received;10.40

解释:现在也为上述代码添加解释。

awk -F";" '         ##Setting field separator as semi colon here.
$4~/[0-9]/{         ##Checking condition here if 4th field is having digit in it, if yes then do following:
  $4="-"$4          ##Adding a dash(-) before 4th field value here.
}
{
  gsub(/;+/,";");   ##By Globally substitution method Removing multiple semi colons occurrences with single semi colon here, as per OP shown output.
  sub(/;$/,"")      ##By using normal substitution method replacing semi colon which comes at last of line with NULL here.
}
1                   ##awk works on method of condition{action}, so here I am making condition TRUE and NOT mentioning any action so default print will happen.
' OFS=";" Input_file##Setting OFS(Output field separator) as semi colon here and mentioning Input_file name here too.
6赞 Ed Morton 1/23/2018 #2

我不明白为什么你感到惊讶,当你打印时,它们之间没有 OFS,但我也不明白为什么你试图在脚本中使用逻辑,而不仅仅是:$1$2$3

$ awk 'BEGIN{FS=OFS=";"} {print $1, $2, ($3=="" ? "-"$4 : $3)}' file
15.01.2018;Payment sent;-500.00
20.12.2017;Payment received;10.40

评论

0赞 PesaThe 1/23/2018
如果两者都存在 和 怎么办?$3$4
0赞 Ed Morton 1/23/2018
根据 2 美元的文本值(一个是“发送”的钱,另一个是“收到”的钱),我看不出它们是怎么回事,OP 没有在示例输入/输出中显示这一点。在我看来,OP 只是试图创建一个列,然后她可以在底部求和得出总数。
1赞 PesaThe 1/23/2018
真的,对不起:)
0赞 Claes Wikner 1/23/2018 #3
awk '{sub(/sent;;/,"sent;-")sub(/;;+/,"")}1' file

15.01.2018;Payment sent;-500.00
20.12.2017;Payment received;10.40

第一个子将分号更改为破折号,第二个子删除最后一个零之后的分号。

评论

0赞 Stphane 1/23/2018
对这个 awk 命令集的解释将使这个答案成为一个完美的答案,因为提问者/读者可能对 awk 感到不舒服。简洁是可以接受的,但解释越充分越好。
1赞 Jkimbu 7/9/2022 #4

这些答案实际上都没有回应OP的问题。问题是“为什么 OFS 没有出现在输出中?答案很简单,一个人在正确的方向上发表了尖刻的评论,但实际上没有人回答。

答案是:在...打印 $1$2$3...部分,$1、$2 和 $3 之间没有空格,因此您要求 awk 将这些字段彼此相邻,没有空格或字段分隔符。如果你有......打印 1,2 美元,3 美元。然后你就会得到你想要的结果。

是的,我知道这是一个老生常谈的问题

评论

0赞 RARE Kpop Manifesto 7/9/2022
甚至根本不应该用于此任务;FS / OFS
0赞 RARE Kpop Manifesto 7/9/2022 #5

使用作为 either 或";"FSOFS

{m,g}awk 'sub(";*$",_,$!(NF=NF))' FS='sent;+' OFS='sent;-'  
{m,g}awk NF=NF RS=';*\r?\n' FS='sent;+' OFS='sent;-' 
15.01.2018;Payment sent;-500.00
20.12.2017;Payment received;10.40