LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Removing new line characters on every line execpt first line (http://www.linuxquestions.org/questions/linux-newbie-8/removing-new-line-characters-on-every-line-execpt-first-line-677767/)

bioinformatics_guy 10-20-2008 06:56 AM

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.

pixellany 10-20-2008 08:13 AM

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

bioinformatics_guy 10-21-2008 08:01 AM

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?

bioinformatics_guy 10-21-2008 08:20 AM

I was a little cheeky and did the following:

awk 'NR>1' <filename> | tr -d '\n' > <newfilename>

pixellany 10-21-2008 12:41 PM

Quote:

Originally Posted by bioinformatics_guy (Post 3317524)
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.


All times are GMT -5. The time now is 07:56 PM.