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


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 !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-22-2012
Registered User
 
Join Date: Nov 2009
Posts: 21
Thanks: 6
Thanked 0 Times in 0 Posts
Search and replace command

Hi,
I wanted to replace the character '~' with \n (carraige retrurn or new line character)

I am tried the commands like


Code:
s/~/\n/g
 
s/~//\n/g

but the commands says that no "No match exists for the substitute pattern".

Last edited by methyl; 06-22-2012 at 07:19 AM..
Sponsored Links
    #2  
Old 06-22-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,370
Thanks: 87
Thanked 476 Times in 456 Posts

Code:
sed 's/~/\
/g'

Sponsored Links
    #3  
Old 06-22-2012
Registered User
 
Join Date: Nov 2009
Posts: 21
Thanks: 6
Thanked 0 Times in 0 Posts
Hi,
Thanks for your reply, But I dont see any carriage return character metioned in your command.
Moreover I tried the command that you provided but i dont see the result. Could you please suggest me anyother ways of doing it.

My i/p file will look like


Code:
ISA*00**00**02*CN*ZZ*RECEIVERID*060628*0035*U*00201*000000612*0*P*> ~GS*IM*CN*APPLICATION RECEIVER ID*20060628*0035*612*X*004010 ~ST*210*612001~B3*B*28061234*102141*PP**20060628*208360****CNRU ~N1*PR*PAYERNAME*25*772305B~N3*123NEWBRIDGEROAD .~N4*ETOBICOKE*ON ~N1*CN*CONSIGNEENAME*25*772305~N3*1800INKSTERBLVD~N4*WINNIPEG SYMING YAR*MB*R2X2Z5 ~N1*SF*SHIP FROM NAME*25*772305 ~

I have change this as


Code:
ISA*00**00**02*CN*ZZ*RECEIVERID*060628*0035*U*00201*000000612*0*P*> 
GS*IM*CN*APPLICATION RECEIVER ID*20060628*0035*612*X*004010 
ST*210*612001
B3*B*28061234*102141*PP**20060628*208360****CNRU 
N1*PR*PAYERNAME*25*772305B
N3*123NEWBRIDGEROAD .
N4*ETOBICOKE*ON 
N1*CN*CONSIGNEENAME*25*772305
N3*1800INKSTERBLVD
N4*WINNIPEG SYMING YAR*MB*R2X2Z5 
N1*SF*SHIP FROM NAME*25*772305


Last edited by methyl; 06-22-2012 at 07:15 AM.. Reason: Please use code tags.
    #4  
Old 06-22-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,370
Thanks: 87
Thanked 476 Times in 456 Posts
You see the back-slash in the command? Right after it, I have pressed Enter (or Return). So, I am escaping the newline and telling sed to treat it as a new-line character for replacement...
Sponsored Links
    #5  
Old 06-22-2012
Registered User
 
Join Date: Nov 2009
Posts: 21
Thanks: 6
Thanked 0 Times in 0 Posts
It gives syntax error for when I execute it in a script as you have mentioned.
Sponsored Links
    #6  
Old 06-22-2012
methyl methyl is offline Forum Staff  
Moderator
 
Join Date: Mar 2008
Posts: 6,388
Thanks: 286
Thanked 668 Times in 640 Posts
Try the unix tr command:


Code:
cat inputfile | tr '~' '\n' > outputfile

I get a virtually perfect match to your sample output (including sporadic trailing space characters), except that the very last ~ causes a trailing blank line in the output file.

Last edited by methyl; 06-22-2012 at 08:00 AM.. Reason: typos in wording
Sponsored Links
    #7  
Old 06-22-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,341
Thanks: 144
Thanked 1,754 Times in 1,591 Posts
Elixir_sinari's suggestion should work fine. Make sure the backslash is the very last character on the first line.

Alternative sed that does allow \n :

Code:
sed 'y/~/\n/' infile

Sponsored Links
Closed Thread

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
search and replace with sed command divya bandipotu Shell Programming and Scripting 2 06-16-2011 04:21 AM
awk - replace number of string length from search and replace for a serialized array otrotipo Shell Programming and Scripting 1 07-10-2009 12:04 PM
How to search and replace a particular line in file with sed command sshah1001 UNIX for Dummies Questions & Answers 2 05-30-2009 10:45 PM
SED command to search for numeric and replace ajayh UNIX for Dummies Questions & Answers 1 04-16-2009 01:35 PM
sed search and replace command janzper Shell Programming and Scripting 1 01-26-2009 10:49 AM



All times are GMT -4. The time now is 04:05 PM.