Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shell scripts find n replace the word in files

 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I would like to know how to find n replace the word in files the particular file format which is avail in different directories. The file should be identify by using the file extention (.txt).
I know using shell script we can achieve this task. Can anyone help me out.
Thanks
Cheers
Jowsaki
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
find <dir1> <dir2> <dir3> -name '*.txt' -exec perl -pi.bak -e 's/\bFIND\b/REPLACEMENT/g' {} \;
or (maybe more efficient But...)
perl -pi.bak -e 's/\bFIND/REPLACEMENT/g' $(find dir -name '*.txt')
Just reaplce WORD and REPLACEMENT with your needs. Perl will do an in-place-edit of the original file, saving a backup to orig.bak.
If you don't want the backups, simply remove the '.bak' argument from the -i switch.
 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
THanks for your response.
Yes, the second command is works fine, if I define the dir name.
Cheers
Jowsaki
 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Leslie,
If I want to change the filename in multiple directories.
Say (*.txt to *.lcd)
Thanks
Cheers
Jowsaki
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
find <dir1> -name '*.txt' | sed 's/\(.*\).txt/mv & \1.lcd/' | sh
[ November 24, 2003: Message edited by: Leslie Chaim ]
 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry it is not working n givin an error as cannot execute.
sh: ./2/231825.txt: cannot execute
 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Leslie,
It works perfectly..
Cheers
Jowsaki
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good! so you figured it out yourself
For those who care for the perl counterpart:
find . -name '*txt' | perl -ne '/(.*).txt/; chomp; rename $_, "$1.lcd"'
If you have a large number of files you gain some milliseconds from the perl version
 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Leslie,
Thanks for all your support.
I hv requirement like this (find n replacement) but now the filenames are limited and the filesnames list are updated in one files.
like (fn.txt has all filenames has to be changed)
aa
bb
cc
xx
yy
zz
now I have to read this file and do the below process only on those files.
perl -pi.bak -e 's/\bFIND/REPLACEMENT/g' $(find dir -name '*.txt')

Can you please help me out in this regards
Thanks
Cheers
Jowsaki
[ December 12, 2003: Message edited by: Sham Jowsaki ]
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand your problem correctly, you'd like to do the same as this

But instead of the command substitution of find command:


You'd like to work with files listed in another file.
The solution is simple: just replace whatever is in the command substitution to read your fn.txt.
You can use either:

Or, take advantage (and impress your peers with shell (built-in) redirection:

Putting this all together you get:

[/code]
perl -pi.bak -e 's/\bFIND/REPLACEMENT/g' $( < fn.txt)
[/code]
HTH.
BTW, why did you think this deserved a private message?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic