PDA

View Full Version : how to get rid of end of line char by using scripts


yan
02-26-03, 16:54
How to use end of line character(\n) as delimiter in sed or awk?
or anyway can solve my problem by using boron shell scripts.
e.g. myfile contains the following message
hello world1
hello world2
I want to append the second line to the first line.
I tried the following ways:
nawk 'BEGIN{FS="\n"} END{print $0}' myfile
and
sed -n 's/\\\n//g' myfile

They all didn't work! sign.... Can anyone help me to figure out the problems?

stevetucknott
02-27-03, 04:14
awk 'BEGIN{RS="/"} {gsub("\n","");print}' filename

This sets the record separtor to a character that doesn't exist in the source file (in my case a slash). The for each line read, strip off the newlines and print. This will concatenate all the lines in the file to one massive output.
If you just wanted to join adjacent lines, then this does not work, and
something along the lines of:
awk '{gsub("\n","");getline x;printf "%-s%-s\n",$0,x}' filename

should work.
This reads a line (automatically), uses getline to 'read' the next line into x, then prints the original row and x followed by a newline.

I'm not an expert so there may be easier ways of doing the same...

sathyaram_s
03-11-03, 13:42
The first script appends pairs of lines ...

cat <filename> | sed '$!N;s/\n/ /'

The following appends all lines in a file to one ...

cat <filename>| sed -e :a -e '$!N;s/\n/ /; ta'

Cheers

Sathyaram