简体   繁体   中英

How to remove new line characters from data rows

I have a file as below

Emp1|FirstName|MiddleName|LastName|Address|Pincode|PhoneNumber
1234|FirstName1|MiddleName2|LastName3| Add1 || ADD2|123|000000000
2345|FirstName2|MiddleName3|LastName4|
Add1 || ADD2|
 234|000000000

OUTPUT:

Emp1|FirstName|MiddleName|LastName|Address|Pincode|PhoneNumber
1234|FirstName1|MiddleName2|LastName3| Add1 || ADD2|123|000000000
2345|FirstName2|MiddleName3|LastName4| Add1 || ADD2|234|000000000

The problem here is not so much How to remove new line characters , but when to remove them. Fortunately, the lines which need to be joined with the following line can be determined by the trailing | . An appropriate sed command is:

sed ':0;/|$/{N;s/\n//;b0}'
  • :0 - label at beginning of script
  • /|$/ - address lines ending with |
  • N - append next line of input
  • s/\n// - delete the embedded newline
  • b0 - branch to beginning of script (to join more lines)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM