search and replace combination of two words...with a constraint


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search and replace combination of two words...with a constraint
# 1  
Old 04-22-2010
search and replace combination of two words...with a constraint


Hi I have 100 files in my directory.
Please help me how to do in Unix or any other scriptin lanuages.
I want to replace all occurances of "goutham" to goutham_ind ONLY if the file contains the word "goutham" with the word "engineer";
for eg----test1 is a file contains the following inf;
goutham engineer
test2 is a file contains the following inf;
goutham softwareengineer
Here i want to replace "goutham" to "goutham_ind" when it contains only engineer.Not softwareengineer;

the final result should be;
Test1;
goutham_ind engineer;
pls pls pls help me...

# 2  
Old 04-22-2010
Assuming your file has only 2 columns as posted:
Code:
awk '$1=="goutham" && $2=="engineer" {$1=$1 "_ind" }1' file

# 3  
Old 04-22-2010
grep -w option will help you search the files with word engineer

Code:
grep -l -s -w engineer *

This will list the files .

Then in a loop you can procees the files to edit using sed

Code:
sed 's/goutham/gautham_india/g'

# 4  
Old 04-22-2010
using PERL:-

Code:
perl -wlpe '/\bgoutham\b/ and /\bengineer\b/ and s/engineer/$&_ind/gi;' infiles.txt

# 5  
Old 04-22-2010
hi thanks for your reply,but my requirement is if goutham and only engineer word satisfy then only it replace goutham to goutham_ind.and they are not columns..there are 100 files in a directory and in some files contains goutham and engineer and goutham and softwareengineer.I want to update goutham to goutham_ind if engineer word alone occurs with goutham.
# 6  
Old 04-22-2010
if the files have the same suffix like txt use below were the -i option will modify the files itself:-

Code:
perl -i -wlpe '/\bgoutham\b/ and /\bengineer\b/ and s/goutham/$&_ind/gi;' *.txt

SmilieSmilieSmilie
# 7  
Old 04-22-2010
hi Ahmed.....thanks alot..you r realy star....got it...thanks alot again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Create 'n' number random pairwise combination of words

File 1 contains the list of words that needed to be randomly paired: Tiger Cat Fish Frog Dog Mouse Elephant Monkey File 2 contains the pairs that should not be used (in any solution) during random pairing. Elephant-Dog Cat-Fish Monkey-Frog Dog-Elephant, Fish-Cat, Frog-Monkey... (1 Reply)
Discussion started by: sammy777888
1 Replies

2. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

3. Shell Programming and Scripting

Search words in any quote position and then change the words

hi, i need to replace all words in any quote position and then need to change the words inside the file thousand of raw. textfile data : "Ninguno","Confirma","JuicioABC" "JuicioCOMP","Recurso","JuicioABC" "JuicioDELL","Nulidad","Nosino" "Solidade","JuicioEUR","Segundo" need... (1 Reply)
Discussion started by: benjietambling
1 Replies

4. UNIX for Dummies Questions & Answers

Replace the words in the file to the words that user type?

Hello, I would like to change my setting in a file to the setting that user input. For example, by default it is ONBOOT=ON When user key in "YES", it would be ONBOOT=YES -------------- This code only adds in the entire user input, but didn't replace it. How do i go about... (5 Replies)
Discussion started by: malfolozy
5 Replies

5. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

6. Shell Programming and Scripting

Search between two words

Hello, I try to print out with sed or awk the 21.18 between "S3 Temperature" and "GrdC" in a text file. The blanks are all real blanks no tabs. Only the two first chars from temperture are required. So the "21" i need as output. S3 Temperatur 21.18 GrdC No Alarm ... (3 Replies)
Discussion started by: felix123
3 Replies

7. Shell Programming and Scripting

Complex find and replace only 1st instance string with dynamic combination

test.txt is the dynamic file but some of combination are fix like below are the lines ;wonder_off = ;wonder_off = disabled wonder_off = wonder_off = disabled the test.txt can content them in any order #cat test.xt ;wonder_off = ;wonder_off = disabled wonder_off = wonder_off =... (5 Replies)
Discussion started by: SilvesterJ
5 Replies

8. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

9. Shell Programming and Scripting

Search 3 words

Hi All, I have almost 1000+ files and I want to search specific pattern. Looking forwarded your input. Pls note that need to ignore words in between /* */ Search for: "insert into xyz" (Which procedure contain all 3). Expected output: procedure test1 procedure test2 procedure test3 File... (12 Replies)
Discussion started by: susau_79
12 Replies

10. Shell Programming and Scripting

Search and replace words between two keywords

Hi, I have a file which contains the following : select * from test where test_id=1; select id from test1, test2 where test_id=1 and test_id=2; select * from test1, test2, test3 where test_id=4 and test2_id where in (select test2_id from test2); select id1, id2 from test ... (6 Replies)
Discussion started by: vrrajeeb
6 Replies
Login or Register to Ask a Question