4

I have the following data

What i'd like to do is for every line, where there is no ,"555" at the end, I want to remove the the line break, \n, and replace it with space.

So, the 'matching line' would be the line without the "555" at the end. i'd want to replace the line break from those lines.

I am limited in that I can't use Perl for this. So, it has to be AWK, or SED. But preferably AWK as I know SED isn't that good for working on multiple lines.

Here are 2 examples:

Example 1

"abc","555"
"d
e
f","555"
"xyz","555"
"abc
d,e,f ghi
jkl
mnop,qrs","555"

Should become (after amendment)

"abc","555"
" d e f","555"
"xyz","555"
"abc d,e,f ghi jkl mnop,qrs","555"

& Example 2

"abc","555"
"d
e
f","555"
"xyz","555"
"aa
bb
cc
dd","555"

Should become (after amendment)

"abc","555"
"d e f","555"
"xyz","555"
"aa bb cc dd","555"

So in output needed, actually every line has format of

"content","555"
4

The trick, in this case, is not to think of it as “working on multiple lines”.

awk '/"555"$/ {print; next} {printf "%s ", $0}'

prints each line that is the last line of a group normally, and then says next to go on to the next input line (not processing the remaining command).  The remaining command, which becomes a default case, prints the current line, followed by a space, without a newline.  This could also be done as

awk '{if ($0 ~ /"555"$/) print; else printf "%s ", $0}'
  • that is very good, just tested it and it works and it works for \r\n or for \n.. that works for linux or of course, windows with cygwin, but i'll just add in case it's helpful to others-for cmd prompt-(interesting also 'cos normally I often have to do ^" or ^\" but this escaping works- awk "{if ($0 ~ /\"555\"$/) print; else printf \"%s \", $0}" <abcd.txt and awk "/\"555\"$/ {print; next} {printf \"%s \", $0}" <abcd.txt – barlop Oct 4 '14 at 13:53
  • Thanks G-Man that worked. Though initially i was having some errors as i am using GNU AWK on windows and using batch files. I have to add %% instead of % in command to work. Thanks Barlop too :) – SuperUserMan Oct 4 '14 at 17:03
  • @SuperUserMan yep and another troubleshooting point is if you find line breaks get removed then it indicates one hasn't used the correct code. (in this case code is 555. if it were longer it'd be a good idea to copy/paste it). but if line breaks got removed you'd know it was from entering the wrong code ;-) – barlop Oct 4 '14 at 17:07
1

I'm not familiar with AWK, but you could do it in two steps, first replace "555"\n with something that you will never encounter in your file, like "55555555555555555#####################################444444444444444444444444444 ". Then replace all \n with space

Subsequently replace the weird string with "555"\n

  • Sounds like a good plan. Do you have a suggestion of anything you're familiar with from the command line that he could use to replace "555\n" with that thing he'll never encounter? – barlop Oct 3 '14 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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