LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-20-2008, 06:56 AM   #1
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Rep: Reputation: 15
Removing new line characters on every line execpt first line


I have a file of the following form (I've added new line characters to avoid any confusion):

>123456\n
aassfffasfasdfafaadf\n
asdfasdfadsfafda\n
asdfasdfasdfasdfaf\n
asdfsdafadsfasdfadsf\n

etc...

Right I am using the tr command:

tr -d '\n' < filename > newfilename , to get the file but I just realised I don't want the new line character removed from the first line. I'm assuming I'll need to set up some sort of logic with sed, awk, or perl but not sure what to do.

I'm assuming I can just say, if first line, next, else, remove new line character.
 
Old 10-20-2008, 08:13 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Do you mean that you want to wind up with the first line unchanged---and then all other data on one line? That will be the effect of "removing the newlines".

Try this:
sed -n -e '1p' -e '2,$H' -e '${g;s/\n//gp}' filename

Great tutorials here:
http://www.grymoire.com/Unix
 
Old 10-21-2008, 08:01 AM   #3
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
I don't really even need the first line, I just need all the rest of the lines as a single long string. Is this possible?
 
Old 10-21-2008, 08:20 AM   #4
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
I was a little cheeky and did the following:

awk 'NR>1' <filename> | tr -d '\n' > <newfilename>
 
Old 10-21-2008, 12:41 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by bioinformatics_guy View Post
I don't really even need the first line, I just need all the rest of the lines as a single long string. Is this possible?
The command I gave you does that---to not show the first line, just delete the first "-e" expression.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 08:13 AM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
51 characters only in the 1st Line of command line eggCover Linux - General 2 07-29-2004 01:28 PM


All times are GMT -5. The time now is 06:18 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration