Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Search Forums:



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-02-2012
Registered User
 

Join Date: Feb 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Remove a newline char from selected rows.

Greetings!

Can we automate the process of removing a newline char from selected rows in a fixed width file using a shell?
Input is like

Code:
abcd1234
xyzd1234
abcd
a1b2c3d4
abcd1234
xyzd1234
xx
abcd1234

Expected output -

Code:
abcd1234xyzd1234
abcda1b2c3d4abcd1234xyzd1234
xxabcd1234

Note - its like if in above case no of characters in a line -eq 9 (including new line char) remove newline char else not.

Appreciate your help!

Last edited by Franklin52; 02-03-2012 at 01:18 PM.. Reason: Please use code tags for data and code samples, thank you
Sponsored Links
    #2  
Old 02-02-2012
Registered User
 

Join Date: Oct 2010
Location: Bilbao, Spain
Posts: 574
Thanks: 8
Thanked 157 Times in 155 Posts
Hi mailme0205,

I think there are some solutions similar in this forum, but try with this:

Code:
$ cat infile
abcd1234
xyzd1234
abcd
a1b2c3d4
abcd1234
xyzd1234
xx
abcd1234
$ perl -pe 'chomp; printf qq[\n] if length != 8; END { printf qq[\n] }' infile
abcd1234xyzd1234
abcda1b2c3d4abcd1234xyzd1234
xxabcd1234

Regards,
Birei
Sponsored Links
    #3  
Old 02-02-2012
Scrutinizer's Avatar
mother ate her
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 5,382
Thanks: 82
Thanked 1,115 Times in 1,019 Posts
And an awk:

Code:
awk 'END{print RS} length!=8{print RS}1' ORS= infile

And some awks can do:

Code:
awk 'END{print RS} NF!=8{print RS}1' FS= ORS= infile

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
SED: Place char at starting and replace selected line hkansal Shell Programming and Scripting 2 06-11-2009 02:42 AM
Splitting a variable based on newline char pavanlimo Shell Programming and Scripting 3 03-26-2009 02:18 AM
Need to print only selected char in a string..? balan_mca Shell Programming and Scripting 2 10-21-2008 09:50 PM
How to replace any char with newline char. mightysam Shell Programming and Scripting 5 09-18-2008 08:15 PM
print selected rows with awk tonet Shell Programming and Scripting 6 09-27-2007 06:23 AM



All times are GMT -4. The time now is 03:35 AM.