1

I have data:

1528198031|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528202225|Andr|thund|Lorem ipsum dolor sit amet, conse
1) Lorem ipsum dolor sit amet, consectetur adipiscing elit
2) Lorem ipsum dolor sit amet, consectetur adipiscing elit
3) Lorem ipsum dolor sit amet, consectetur adipiscing elit

Lorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elit
1528202574|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528203825|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528203890|Andr|rail|Lorem ipsum dolor sit amet, consectetur adipiscing elit

I want to remove \n in column 4 using sed or awk if it possible. Result would look like this:

1528198031|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528202225|Andr|thund|Lorem ipsum dolor sit amet, conse 1) Lorem ipsum dolor sit amet, consectetur adipiscing elit 2) Lorem ipsum dolor sit amet, consectetur adipiscing elit 3) Lorem ipsum dolor sit amet, consectetur adipiscing elit Lorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elit
1528202574|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528203825|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528203890|Andr|rail|Lorem ipsum dolor sit amet, consectetur adipiscing elit

Patterns before \n always different, it can be 1-9 or text, or symbols like :,;, (, ).

Text was changed, but formatting is like in original data.

2

sed - join to the previous line if this line contains only non-pipe characters:

sed -e :a -e '$!N; s/\n\([^|]*\)$/ \1/; ta' -e 'P;D' file
1528198031|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528202225|Andr|thund|Lorem ipsum dolor sit amet, conse 1) Lorem ipsum dolor sit amet, consectetur adipiscing elit 2) Lorem ipsum dolor sit amet, consectetur adipiscing elit 3) Lorem ipsum dolor sit amet, consectetur adipiscing elit  Lorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elit
1528202574|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528203825|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528203890|Andr|rail|Lorem ipsum dolor sit amet, consectetur adipiscing elit
  • Yeah, that's it. But I don't know why, it doesn't work with file, unless I open it and save manually. Then it works perfect. – Strawberry Hat Jun 13 '18 at 20:52
  • @StrawberryHat your version of sed may have a -i or --in-place option that allows you to modify the contents of the file directly – steeldriver Jun 13 '18 at 22:10
  • tried with -i, still don't work without manually saving file. Tried to split file by 250 lines, change chmod - also doesn't work. upd: Sed version 4.5 March 2018 (that's how man sed print it) – Strawberry Hat Jun 13 '18 at 22:33
  • @StrawberryHat perhaps the original file contains non-printing characters that are getting stripped out when you re-save the file? The usual culprits are carriage returns (from DOS-style CRLF line endings) – steeldriver Jun 13 '18 at 23:13
  • Yeah, you right! Added -e 's/\r//g' before your first -e and it's working now. Thank you! – Strawberry Hat Jun 14 '18 at 0:12
0

Awk solution:

awk -F'|' '{ printf "%s%s", (NR == 1? "" : (NF > 1? ORS : OFS)), $0 }
           END{ print "" }' file

The output:

1528198031|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528202225|Andr|thund|Lorem ipsum dolor sit amet, conse 1) Lorem ipsum dolor sit amet, consectetur adipiscing elit 2) Lorem ipsum dolor sit amet, consectetur adipiscing elit 3) Lorem ipsum dolor sit amet, consectetur adipiscing elit  Lorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elit
1528202574|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528203825|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528203890|Andr|rail|Lorem ipsum dolor sit amet, consectetur adipiscing elit

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.