Find and replace a word in all the files (that contain the word) under a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace a word in all the files (that contain the word) under a directory
# 1  
Old 10-04-2011
Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone,

I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap".

For Example:

In the Directory,
Code:
[xgdfy3487]$pwd
/home/nick

[xgdfy3487] $ grep -l "xsgd1234dap" *.sh | wc -l
119

I have "119" files that are still using "xsgd1234dap" but Instead I would like replace it with "xsdr3423pap" for all the files (119).

Could someone please tell me any easier way to do this instead of manually doing it file by file.

I would really appreciate you time and thoughts.

Last edited by filter; 10-04-2011 at 06:45 PM.. Reason: Adding Code tags
# 2  
Old 10-04-2011
Make a backup first

Then:
Code:
find . -name "*sh" |xargs perl -pi -e 's/find/replace/g'


it was tested on solaris


Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Franklin52; 10-05-2011 at 03:08 AM.. Reason: Please use code tags, thank you
This User Gave Thanks to roche For This Post:
# 3  
Old 10-04-2011
-i.bak will do the backup.

Code:
grep -l "xsgd1234dap" *.sh  |xargs perl -i.bak -pe 's/xsgd1234dap/xsdr3423pap/g'

This User Gave Thanks to rdcwayx For This Post:
# 4  
Old 10-04-2011
Thanks a lot for your replies.

Really solved my problem.

Appreciate your thoughts. Smilie
# 5  
Old 10-04-2011
Code:
 
grep -l "xsgd1234dap" *.sh | while read filename
do
     cp $filename $filename.bk
     sed -i 's/xsgd1234dap/xsdr3423pap/g' $filename
done

# 6  
Old 10-05-2011
No backups. No reading the same file twice. No remorse. Just ed. Smilie

Code:
for f in *.sh; do
    ed -s "$f" <<-'EOED'
        1,$s/xsgd1234dap/xsdr3423pap/g
        w
        q
    EOED
done

Note: The heredoc indentation must consist of tabs (not spaces).


Alternatively:
Code:
for f in *.sh; do
    printf %s\\n '1,$s/xsgd1234dap/xsdr3423pap/g' w q | ed -s "$f"
done


Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find Word in Column with replace (2)

Dear ALL, I have sample file : 4c:66:41:6b:b5:5f, 00:00,00:00, -22:-25, robert, 10.101.3.119, host1 4c:66:41:2c:c1:5a, 00:00,00:00, -21:-25, 10.101.3.112, host1 4c:66:41:6b:b1:5a, 00:00,00:00, -21:-25, Julia, 10.101.3.113, host1 4c:66:41:2c:c1:5b, 00:00,00:00, -21:-25, 10.101.3.115, host1... (2 Replies)
Discussion started by: gnulyn
2 Replies

2. Shell Programming and Scripting

Find Word in Column with replace

Hi ALL i have file.txt with text : 4c:66:41:6b:b5:5f, 00:00,00:00, -22:-25, users1, 10.101.3.119, host1 4c:66:41:2c:c1:5a, 00:00,00:00, -21:-25, 10.101.3.112, host1 4c:66:41:6b:b1:5e, 00:00,00:00, -21:-25, users1, 10.101.3.113, host1 4c:66:41:2c:c1:5b, 00:00,00:00, -21:-25, 10.101.3.115,... (3 Replies)
Discussion started by: gnulyn
3 Replies

3. Shell Programming and Scripting

Find a word and increment the number in the word & save into new files

Hi All, I am looking for a perl/awk/sed command to auto-increment the numbers line in file, P1.tcl: run_build_model sparc_ifu_dec run_drc set_faults -model path_delay -atpg_effectiveness -fault_coverage add_delay_paths P1 set_atpg -abort_limit 1000 run_atpg -ndetects 1000 I would like... (6 Replies)
Discussion started by: jypark22
6 Replies

4. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

5. Shell Programming and Scripting

Replacing a particular word with another word in all the xml's under a particular directory with sed

Hi Folks, Could you please advise what will be the SED command to replace a word in all xml's under a particular directory for example let say I rite now at the following below location $ cd /ter/rap/config now under config directory there will be lots of xml file , now my objective is to... (1 Reply)
Discussion started by: punpun66
1 Replies

6. Shell Programming and Scripting

Find word in a directory files

Hi folks, i have a file in which i have words like below words.txt(file name) and words are arranged likes no spaces between words Apple Banana Carrot And i have list files which have these words in a directory (/tmp/satemp) test1.txt test2.txt test3.txt So if the word 'Apple' is... (3 Replies)
Discussion started by: coolboy98699
3 Replies

7. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

8. Shell Programming and Scripting

Replace a word in a string starting with another word

Hi All, I have a file in which a number of lines are starting with similar first word but different next words. I want to replace the any nth word(not 1st or 2nd) with another word. Eg:- My file contains are like this:- Ram is a boy. Ram is a good boy. Ram plays cricket. Here I want to... (2 Replies)
Discussion started by: mukeshbaranwal
2 Replies

9. UNIX for Dummies Questions & Answers

How to find a word in a all the files in a Directory??

I want to find a specific word present in all the files ina directory....Please tell me the command to be used?? Thanks (6 Replies)
Discussion started by: shikhakaul
6 Replies

10. Shell Programming and Scripting

Shell:Find a word in files in a directory and subdirectories

I'm looking to write a ksh code with will be alble to find a word like 'toto' in all files going from my current directory. eg. /doc ----------->have: text.c which "toto" /doc/usr-------->have: build.pc, help.java which "toto" /doc/usr/cach -->have: test.sh which "toto" /doc/build... (4 Replies)
Discussion started by: yeclota
4 Replies
Login or Register to Ask a Question