1

File contents: (based on topic -> 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",

How to improve the command?:

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

Result:

$ 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$ 

I want to print:

"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"

Please only solution in awk.

Thank you for your help.

5
  • If you don't want to duplicate the last 3 digits of the number, why do you have that in your sub() function? The other question seems to show how to do everything you want, what's wrong with it?
    – Barmar
    Nov 25, 2012 at 10:28
  • This looks like a duplicate of: stackoverflow.com/questions/13547392/…
    – Steve
    Nov 25, 2012 at 10:44
  • @steve, I know those solutions, but I want to do it my way.
    – Tedee12345
    Nov 25, 2012 at 11:03
  • @Barmar, because I wanted to replace the last 3 digits and print a new line.
    – Tedee12345
    Nov 25, 2012 at 11:10
  • When you write && it means you want to duplicate the last 3 digits, since each & will be replaced with the match.
    – Barmar
    Nov 25, 2012 at 11:12

3 Answers 3

1

I think this is what you're looking for:

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

Results:

"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"
4
  • What will happen if the date is November 13?
    – Barmar
    Nov 25, 2012 at 11:14
  • Yes, Fixed. But I don't think the question has anything to do with regex
    – Steve
    Nov 25, 2012 at 11:33
  • @steve, Thank you for a good solution. Your solution works, but you have to add the --posix --> awk --posix -v ORS="" '/"[0-9]{2},[0-9]{3}"/ { sub(/,$/,""); print $0 "\n"; next }1' file It's a shorter solution will work -> awk --posix -v ORS="" '/,[0-9]{3}/{ sub(/,$/,""); print $0 "\n"; next }1' file.
    – Tedee12345
    Nov 25, 2012 at 11:43
  • You're adding --posix to get RE-intervals to work. That functionality is enabled by default in newer gawk versions (check the manuals for which) so you don't need it there and in older gawk versions you should use --re-interval instead of --posix so you don't disable all gawk extenisons like gensub() and time functions.
    – Ed Morton
    Nov 26, 2012 at 16:23
1

This one liner will work,

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

Here is a more elaborated version.

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

I know you said only awk. If you're willing to stretch:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.