File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Linux / UNIX and the fly likes Shell scripts find n replace the word in files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Shell scripts find n replace the word in files" Watch "Shell scripts find n replace the word in files" New topic
Author

Shell scripts find n replace the word in files

Sham Jowsaki
Ranch Hand

Joined: May 22, 2001
Posts: 146
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

Joined: May 22, 2002
Posts: 336
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.


Normal is in the eye of the beholder
Sham Jowsaki
Ranch Hand

Joined: May 22, 2001
Posts: 146
Hi,
THanks for your response.
Yes, the second command is works fine, if I define the dir name.
Cheers
Jowsaki
Sham Jowsaki
Ranch Hand

Joined: May 22, 2001
Posts: 146
Hi Leslie,
If I want to change the filename in multiple directories.
Say (*.txt to *.lcd)
Thanks
Cheers
Jowsaki
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
find <dir1> -name '*.txt' | sed 's/\(.*\).txt/mv & \1.lcd/' | sh
[ November 24, 2003: Message edited by: Leslie Chaim ]
Sham Jowsaki
Ranch Hand

Joined: May 22, 2001
Posts: 146
Sorry it is not working n givin an error as cannot execute.
sh: ./2/231825.txt: cannot execute
Sham Jowsaki
Ranch Hand

Joined: May 22, 2001
Posts: 146
Thanks Leslie,
It works perfectly..
Cheers
Jowsaki
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
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

Joined: May 22, 2001
Posts: 146
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

Joined: May 22, 2002
Posts: 336
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shell scripts find n replace the word in files