1

文件内容:(基于主题 -> 1

"Shimshon A",
"(blank)",
"November 24, 2012",
"13,481",
"jonathan t",
"Laguna Niguel, CA",
"November 24, 2012",
"13,480",
"scott b",
"Sussex, NJ",
"November 24, 2012",
"13,479",

如何改进命令?:

gawk --posix 'ORS="";{sub(/,[0-9]{3}/, "&&\n"); print }' file

结果:

$ gawk --posix 'ORS="";{sub(/,[0-9]{3}/, "&&\n"); print }' file
"Shimshon A","(blank)","November 24, 2012","13,481,481
","jonathan t","Laguna Niguel, CA","November 24, 2012","13,480,480
","scott b","Sussex, NJ","November 24, 2012","13,479,479
",userpc@userpc-desktop:~/Pulpit$ 

我想打印:

"Shimshon A","(blank)","November 24, 2012","13,481"
"jonathan t","Laguna Niguel, CA","November 24, 2012","13,480"
"scott b","Sussex, NJ","November 24, 2012","13,479"

请仅在 awk 中解决。

感谢您的帮助。

4

3 回答 3

1

这一个班轮将工作,

awk -FS="" 'BEGIN{ORS=""} {print substr($1, 1, length($1)-1) ((c%4==3)? "\n": ",");c=c+1;}' file

这是一个更详细的版本。

awk -FS="" 'BEGIN{
    ORS="";
    c=0
} {
    print substr($1, 1, length($1)-1) ((c%4==3)? "\n": ",");
    c=c+1;
}' < file
于 2012-11-25T10:44:07.607 回答
1

我想这就是你要找的:

awk -v ORS="" '/"[0-9]{2},[0-9]{3}"/ { sub(/,$/,""); print $0 "\n"; next }1' file

结果:

"Shimshon A","(blank)","November 24, 2012","13,481"
"jonathan t","Laguna Niguel, CA","November 24, 2012","13,480"
"scott b","Sussex, NJ","November 24, 2012","13,479"
于 2012-11-25T11:11:30.307 回答
1

我知道你说的只是awk。如果你愿意拉伸:

paste -d "" - - - - < file | sed 's/,$//'
于 2012-11-25T14:20:13.253 回答