find from file and replace with specific text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find from file and replace with specific text
# 1  
Old 05-09-2012
Data find from file and replace with specific text

Dear All,

I do not have any knowledge of scripting. I want to replace specific lines of a text file with a specific text. Like I have one file which is "original file" and one file "changes file" which has list of lines which I want to replace in original file with a specific string. I want the script to go through the original file using changes file line by line and change the text as soon as it finds it with the given text and after first occurrence should move to the next line of the changes file and look for the second line and change it with the same given text. the lines in the changes file are kept sequentially in the same order as the original file so the pointer do not need to go back up. Please help. Thanks in advance.
# 2  
Old 05-09-2012
Bug

Code:
cat old_file | sed 's/one/two/g' > new_file

# 3  
Old 05-09-2012
Please post an example of both files where we can see by which key/criteria the replacement should be done.
Please use [code] and [/code] tags for better readability of the examples, thanks.

@pamu:
You can leave that cat out. It is not needed since sed like many other tools can read files on their own; example:
Code:
sed 's/one/two/g' old_file > new_file

# 4  
Old 05-09-2012
I will try to explain with following example.


original_file
Code:
a@b.com
c@d.com
E@f.com
g@h.com
i@j.com
k@l.com

change_file
Code:
c@d.com
i@j.com

I want to change the contents of original_file that matches contents of change_file with specific text like x@x.com
After running the query the resulting file should look like following

New_file
Code:
a@b.com
x@x.com
E@f.com
g@h.com
x@x.com
k@l.com

I hope this explains my requirement. Thanks alot for all the support.

Moderator's Comments:
Mod Comment Please use code tags, thanks!

Last edited by zaxxon; 05-09-2012 at 07:39 AM.. Reason: code tags
# 5  
Old 05-09-2012
try this
Code:
awk -v val="x@x.com" 'NR==FNR{a[$1]++;next}
a[$1]{$0=val}1' changeFile origfile

This User Gave Thanks to pravin27 For This Post:
# 6  
Old 05-09-2012
Thanks alot, it worked like magic.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find replace text in xml file on the fly

Dear Unix guru, I have a .XML file which is being used to load data to oracle. This file comes on unix box and one of the tag in xml is oracle key word. I want to find that tag and replace with new tag on the fly For example I will get one of the tag in xml is as below <from>Test Test... (12 Replies)
Discussion started by: guddu_12
12 Replies

2. UNIX for Beginners Questions & Answers

Find and replace a string in a text file

Dear all, I want to find all the "," in my text file and then replace the commas to a tab. I found a script online but I don't know how to modify the script for my case. Any one can help? Thank you. @echo off &setlocal set "search=%1" set "replace=%2" set "textfile=Input.txt" set... (2 Replies)
Discussion started by: forevertl
2 Replies

3. Shell Programming and Scripting

Error while executing switch case for find and replace specific file. Help Me...

case "$inputs" in sapte) find /pools/home_unix/sapte/work/models/model -name "*.xml" exec rm -i {} \;; ckm1) find /pools/home_unix/ckm1/work/models/model -name "*.xml" exec rm -i {} \;; I am getting error like as below. ./menu1.sh: line 144: syntax error near unexpected token `)'... (4 Replies)
Discussion started by: lathigara
4 Replies

4. UNIX for Advanced & Expert Users

Find and replace the line in text file

I have two files a.txt b.txt I want to find a line in a.txt and replace by another line from b.txt a.txt asfsdfsfsfdfsf asfwererfgdgf wrerwetretfdg b.txt werdfgdfgf werergfdgd sfdfgfgfgfgg i want to replace the 1st line of a.txt by 1st line of b.txt i want out put as (5 Replies)
Discussion started by: rammm
5 Replies

5. UNIX for Dummies Questions & Answers

Use sed to replace but only in a specific column of the text file

Hi, I would like to use sed to replace NA to x ('s/NA/x/g'), but only in the 5th column of the space delimited text file, nowhere else. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

6. Shell Programming and Scripting

Need an awk for a global find/replace in a file, specific column

I am new to unix and awk/sed etc... using C-Shell. Basically, I have a fixed length file that has 4 different record types on it, H, D, V, W all in column 1. I need to change all the W's in column 1 to D's. in the entire file. The W's can be anywhere in the file and must remain in the same... (3 Replies)
Discussion started by: jclanc8
3 Replies

7. Shell Programming and Scripting

Find and replace a string a specific value in specific location in AIX

Hi, I have following samp.txt file in unix. samp.txt 01Roy2D3M000000 02Rad2D3M222222 . . . . 10Mik0A2M343443 Desired Output 01Roy2A3M000000 02Rad2A3M222222 . . (5 Replies)
Discussion started by: techmoris
5 Replies

8. UNIX for Dummies Questions & Answers

how to use vi to replace specific line of text only

hi all, i am new bee to Unix. i know how to replace text for range of lines in vi for e.g. replace | with |||| for line 4 through 7 using vi : 4,7s/|/||||/g but i have to replace | with |||| only for line no 4, 7 and 10 using vi only!!! your help will be appreciated! thanks,... (4 Replies)
Discussion started by: pranav.pandya
4 Replies

9. UNIX for Dummies Questions & Answers

search and replace a specific text in text file?

I have a text file with following content (3 lines) filename : output.txt first line:12/12/2008 second line:12/12/2008 third line:Y I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before. I tried using cat output.txt... (4 Replies)
Discussion started by: santosham
4 Replies

10. Shell Programming and Scripting

read space filled file and replace text at specific position

Hi I have a spaced filled file having records like below: What I want is to read line having RT3 at position 17-19 then go to position 2651 check the 18 characters (might be space filled till 18 characters). This position should have a... (6 Replies)
Discussion started by: COD
6 Replies
Login or Register to Ask a Question