If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > how to get rid of end of line char by using scripts

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-26-03, 15:54
yan yan is offline
Registered User
 
Join Date: Feb 2003
Posts: 35
how to get rid of end of line char by using scripts

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?
Reply With Quote
  #2 (permalink)  
Old 02-27-03, 03:14
stevetucknott stevetucknott is offline
Registered User
 
Join Date: Jan 2003
Posts: 19
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...
Reply With Quote
  #3 (permalink)  
Old 03-11-03, 12:42
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,543
Re: how to get rid of end of line char by using scripts

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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On