Search and replace command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search and replace command
# 1  
Old 06-22-2012
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 08:19 AM..
# 2  
Old 06-22-2012
Code:
sed 's/~/\
/g'

# 3  
Old 06-22-2012
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 08:15 AM.. Reason: Please use code tags.
# 4  
Old 06-22-2012
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...
# 5  
Old 06-22-2012
It gives syntax error for when I execute it in a script as you have mentioned.
# 6  
Old 06-22-2012
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 09:00 AM.. Reason: typos in wording
# 7  
Old 06-22-2012
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

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to search and replace string in column in file with command sed?

how to search and replace string in column in file with command sed or other search "INC0000003.in" and replace column 4 = "W" $ cat file.txt INC0000001.in|20150120|Y|N|N INC0000002.in|20150120|Y|N|N INC0000003.in|20150120|Y|N|N INC0000004.in|20150120|Y|N|Noutput... (4 Replies)
Discussion started by: ppmanja3
4 Replies

2. Shell Programming and Scripting

Nested search in a file and replace the inner search

Hi Team, I am new to unix, please help me in this. I have a file named properties. The content of the file is : ##Mobile props east.url=https://qa.east.corp.com/prop/end west.url=https://qa.west.corp.com/prop/end south.url=https://qa.south.corp.com/prop/end... (2 Replies)
Discussion started by: tolearn
2 Replies

3. UNIX for Dummies Questions & Answers

Help with search and replace or search only of / in vi

Hi all, I am editing a config file in vi that has a / on it. At the moment, search and replace looks alright as am able to use a # as a temporary separator, i.e. :,$s#/u01/app#/u02/app#g For doing a search, I have to escape the / do. So if I want to search for /u01/app, I am having to do... (2 Replies)
Discussion started by: newbie_01
2 Replies

4. Shell Programming and Scripting

search and replace with sed command

hi, suggest me in the below script.. if In above I wanna replace "" with "]". (2 Replies)
Discussion started by: divya bandipotu
2 Replies

5. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

6. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

7. UNIX for Dummies Questions & Answers

How to search and replace a particular line in file with sed command

Hello, I have a file and in that, I want to search for a aprticular word and then replace another word in the same line with something else. Example: In file abc.txt, there is a line <host oa_var="s_hostname">test</host> I want to search with s_hostname text and then replace test with... (2 Replies)
Discussion started by: sshah1001
2 Replies

8. UNIX for Dummies Questions & Answers

SED command to search for numeric and replace

Hi I am very new to linux and scripting. I need to replace numbers abc with number xyz inputting from a reference file. I used the following command - sed "s/$grd/$lab/" , where $grd and $lab comes from reference file. The problem is the above line doesnt take care of space..... (1 Reply)
Discussion started by: ajayh
1 Replies

9. Shell Programming and Scripting

sed search and replace command

Hi, I would like to seek help on how i can arrive on this result. sample.txt: product_code IN (param001) and product_type IN (param004) product_code IN (param002) and product_type IN (param005) product_code IN (param003) and product_type IN (param006) I would like to change the param001... (1 Reply)
Discussion started by: janzper
1 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question