Results 1 to 5 of 5
I know how to do grep, that's very nice.
But I want to search a folder and it's subfolders and REPLACE a line of code in a file, e.g.
this ...
- 01-08-2008 #1Just Joined!
- Join Date
- Apr 2003
- Posts
- 52
how to replace a line of code something like grepreplace maybe??
I know how to do grep, that's very nice.
But I want to search a folder and it's subfolders and REPLACE a line of code in a file, e.g.
this line
if($qr['count']>1){
with this one
if($qr['count']>1 && !$usemod_settings['allowDuplicateEmails']){
How do you do that in linux?
Thanks,
Samuel
- 01-08-2008 #2Just Joined!
- Join Date
- Nov 2007
- Location
- Germany
- Posts
- 37
You can do that with sed.
The syntax shouldbe something like
sed s/pattern/itsreplace/[Mode] file
Look in the manpage for the details, since i didn't used that before...
Ogion
- 01-08-2008 #3Just Joined!
- Join Date
- Jan 2008
- Location
- Redding, CA
- Posts
- 20
Use sed >= 3.95 so you can edit the files directly with -i switch.
IE -
sed -i 's?old string?new string?g' /path/to/file
if you have a whole directory - somethiling like
find /path/to/directory -type f -print |while read file; do
sed -i 's?old string?new string?g' "${file}"
done
would work.
Of course back up the directory first.
I always use a ? instead of / as a delimiter so I don't have to escape the / character in the expression.
- 01-09-2008 #4Linux User
- Join Date
- Aug 2006
- Posts
- 458
if you know the file names
for one directory levelCode:sed -i 's/cat/dog/' file*.sh
Code:sed -i 's/cat/dog/' */file*.sh
- 01-12-2008 #5Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044


Reply With Quote
