Replace string with modifying original file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace string with modifying original file
# 1  
Old 07-16-2012
Replace string with modifying original file

I have a file abc.txt with following data

Code:
121941602P911000497^123.001^^^^^^^^^0^10^OTHER
121941602P911000497^123.002^^^^^^^^^1^10^OTHER
121941602P911000497^123.003^^^^^^^^^2^10^OTHER
121941602P911000497^123.004^^^^^^^^^3^10^OTHER
121941602P911000497^123.005^^^^^^^^^4^10^OTHER

I want to change the content as
Code:
121941602P911000497^555.001^^^^^^^^^0^10^OTHER
121941602P911000497^555.002^^^^^^^^^1^10^OTHER
121941602P911000497^555.003^^^^^^^^^2^10^OTHER
121941602P911000497^555.004^^^^^^^^^3^10^OTHER
121941602P911000497^555.005^^^^^^^^^4^10^OTHER

If it is single file i can do it in vi editor using find and replace options but i am having n number of files like this i need a shortcut to replace all at once
Code:
My shell is csh

# 2  
Old 07-16-2012
Code:
# awk -F"[\.^]" '{$2=555; print}' OFS=\^ blafile
121941602P911000497^555^001^^^^^^^^^0^10^OTHER
121941602P911000497^555^002^^^^^^^^^1^10^OTHER
121941602P911000497^555^003^^^^^^^^^2^10^OTHER
121941602P911000497^555^004^^^^^^^^^3^10^OTHER
121941602P911000497^555^005^^^^^^^^^4^10^OTHER

Done with GNU awk (gawk); if you get problems try using nawk instead. To process many files, use a for-loop or something similar around this and redirect the output with a > to a temporary file. You will have to mv the temporary file of course or use sed -i or perl -i to edit the files in place without using a temporary file.

Edit:
Just read the subject again - you don't want to modify the original, then just redirect the output to a new filename.

Last edited by zaxxon; 07-16-2012 at 10:32 AM..
# 3  
Old 07-16-2012
Sorry guys its a typo error in heading
I need to modify in original file

---------- Post updated at 07:14 PM ---------- Previous update was at 07:07 PM ----------

Also using the above code 123.001 is replaced as 555^001 i dont want to change . as ^
# 4  
Old 07-16-2012
using sed:

Code:
$ cat testfil
121941602P911000497^123.001^^^^^^^^^0^10^OTHER
121941602P911000497^123.002^^^^^^^^^1^10^OTHER
121941602P911000497^123.003^^^^^^^^^2^10^OTHER
121941602P911000497^123.004^^^^^^^^^3^10^OTHER
121941602P911000497^123.005^^^^^^^^^4^10^OTHER

Code:
sed -e 's/[0-9]\{1,3\}\./555./g' testfil >> testfil2

you can also use the -i option to change the original file instead of redirect as well. make sure to take back up before you try it out.

Code:
cat testfil2
121941602P911000497^555.001^^^^^^^^^0^10^OTHER
121941602P911000497^555.002^^^^^^^^^1^10^OTHER
121941602P911000497^555.003^^^^^^^^^2^10^OTHER
121941602P911000497^555.004^^^^^^^^^3^10^OTHER
121941602P911000497^555.005^^^^^^^^^4^10^OTHER

# 5  
Old 07-16-2012
True, just rolled over the dot:
Code:
# sed 's/...\./555./' blafile
121941602P911000497^555.001^^^^^^^^^0^10^OTHER
121941602P911000497^555.002^^^^^^^^^1^10^OTHER
121941602P911000497^555.003^^^^^^^^^2^10^OTHER
121941602P911000497^555.004^^^^^^^^^3^10^OTHER
121941602P911000497^555.005^^^^^^^^^4^10^OTHER

Already written how to modify the file. If your sed has a -i switch, then use it or try any of the alternatives that have been described.
# 6  
Old 07-16-2012
I don't know csh, so I'll leave the looping to you, but here's an approach that edits the files in place.
Code:
printf '%s\n' '1,$s/[[:digit:]]*\./555./' w q | ed -s filename

It looks for the first dot in the record and replaces any digits that precede it (if any).

Regards,
Alister
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

3. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

4. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

5. Shell Programming and Scripting

perl script to replace the text in the original file

Hi Folks, I have an html file which contains the below line in the body tagI am trying the replace hello with Hello Giridhar programatically. <body> <P><STRONG><FONT face="comic sans ms,cursive,sans-serif"><EM>Hello</EM></FONT></STRONG></P> </body> I have written the below code to... (3 Replies)
Discussion started by: giridhar276
3 Replies

6. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

7. UNIX for Dummies Questions & Answers

Search a string in the file and then replace another string after that position

Hi I am looking for a particular string in a file.If the string exists, then I want to replace another string with some other text.Once replaced, search for the same text after that character position in the file. :wall: E.g: Actual File content: Hello Name: Nitin Raj Welcome to Unix... (4 Replies)
Discussion started by: dashing201
4 Replies

8. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

9. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

10. Shell Programming and Scripting

How To Replace A String In File With A String Containing Windows File Path

Hi, I have a file with the following contents # Lines that start with a # are comments. # # Calling TOAD like this will perform a comparison from command line : # # "C:\Program Files\Quest Software\Toad for Oracle 9.6\toad.exe" -c... (2 Replies)
Discussion started by: rajan_san
2 Replies
Login or Register to Ask a Question