awk - 在正则表达式后插入一个新行?

时间:2012-11-25 10:16:01

标签: regex awk

文件内容:(基于主题 - > 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中解决。

感谢您的帮助。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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"

答案 2 :(得分:1)

我知道你只说awk。如果你愿意伸展:

paste -d "" - - - - < file | sed 's/,$//'