Computing.Net > Forums > Unix > sed: conditional newline removal

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

sed: conditional newline removal

Reply to Message Icon

Name: uny
Date: September 3, 2009 at 04:03:46 Pacific
OS: Unix
Product: Symantec /
Subcategory: General
Comment:

Conditional newline removal:
I have a requirement as follow:
To remove the newline characters for a fixed width length file of 20 chars.
for example:
cat 1.txt
1234567890
1234567890
09876543210987654321

the output should be:
12345678901234567890
09876543210987654321

The first line should join the second line due to it not reaches the width of 20 chars. After it reaches 20 chars, it should be a new line. The idea is to remove the newline char when in between text.

Any solution?
Thanks in advance



Sponsored Link
Ads by Google

Response Number 1
Name: user57
Date: September 8, 2009 at 07:16:20 Pacific
Reply:

A partial reply...

This will remove the NL character.

sed 'N;s/\n//' datafile

therefore
1234567890
1234567890

becomes
12345678901234567890

This will print lines of less than 21 chars and simply ignores those lines that are longer:
sed -n '/^.\{21\}/!p'


0

Response Number 2
Name: uny
Date: September 9, 2009 at 01:17:36 Pacific
Reply:

Thanks for the partial reply though it may not be a final one...
i got your idea but it seems it is really hard to combine the two sed command to get it done in one step, isn't it? I had been thinking for a long time also...
anyways, thanks for the partial solution, appreciate that!


0

Response Number 3
Name: psiva_unix
Date: September 12, 2009 at 23:38:44 Pacific
Reply:

Hi,

use the below command to remove new line character in the text file
sed 'N:s/\n//g' <file.txt>


0

Response Number 4
Name: uny
Date: September 18, 2009 at 10:08:37 Pacific
Reply:

thanks alot!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: sed: conditional newline removal

Conditional newline removal via sed www.computing.net/answers/unix/conditional-newline-removal-via-sed/7379.html

How sed can remove newline www.computing.net/answers/unix/how-sed-can-remove-newline/6593.html

sed/awk - line feeds? www.computing.net/answers/unix/sedawk-line-feeds/5237.html