Operating System - HP-UX
1752590 Members
4057 Online
108788 Solutions
New Discussion юеВ

Re: change contents in files using grep ?

 
SOLVED
Go to solution
OzTEXS
Advisor

change contents in files using grep ?

Hi Guys,

I have a directory with 30+ files that look like this >>

"binary
get "syutfoft/forec1p" tfile1.zip
quit"

I want to change syufoft to syhfoft on each file automatically.

How can this be done ??

11 REPLIES 11
Oviwan
Honored Contributor
Solution

Re: change contents in files using grep ?

hey

this create an file.old and replace it in each file of the directory.

for file in $(ls /directory/of/your/files) ; do
perl -i.old -ple "s/syufoft/syhfoft/" $file
done

regards
Oviwan
Honored Contributor

Re: change contents in files using grep ?

btw:
>change contents in files using grep ?
check man grep and you get this:
"grep, egrep, fgrep - search a file for a pattern"

so with grep you cannot change the content of a file...
Laurent Menase
Honored Contributor

Re: change contents in files using grep ?

perl is useless there

for file in /directory/of/your/files/* ; do
echo "1,\$s/syufoft/syhfoft/\nw\nq" |ed -s $file
done
James R. Ferguson
Acclaimed Contributor

Re: change contents in files using grep ?

Hi:

> Laurent: perl is useless there

I think you meant to answer the poster's original question of can you "change contents in files using grep?".

Obviously, that answer is "no". However, Perl's inplace update ('-i') as Oviwan used, *will* effect the change.

If you simply use '-i' without the argument ".old" as '-i.old', then no backup copy of the original file suffixed with ".old" will be made.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: change contents in files using grep ?

>change contents in files using grep?

All you can do with grep is use -v to remove whole lines.

To change files, you typically use sed(1) (or Oviwan's and JRF's perl).
for file in $(ls /directory/of/your/files) ; do
sed 's/syufoft/syhfoft/' $file > $file.new
mv $file.new $file
done
Bill Hassell
Honored Contributor

Re: change contents in files using grep ?

Or you can use chgafile...

The attached script will do exactly what your want. I wrote when I had the same task as you described. It has several options including showing the before and after lines that are changed. Without the -w, you get to preview exactly what will be changed.


Usage: chgafile [ -w ] [ -q ] [ -s ] old-string new-string [ filename(s) ]
-w to write changes (default = preview only)
-q quiet mode (no before/after lines)
-s silent (no output at all except for file or STDOUT)
-s also turns on -q for no output at all

old-text and new-text are the strings

one or more files to change or none for STDIN


Bill Hassell, sysadmin
OzTEXS
Advisor

Re: change contents in files using grep ?

but all the files in the directory have different names ?? does this effect the script below ??

for file in /directory/of/your/files/* ; do
echo "1,\$s/syufoft/syhfoft/\nw\nq" |ed -s $file
done

??
OzTEXS
Advisor

Re: change contents in files using grep ?

this create an file.old and replace it in each file of the directory.

for file in $(ls /directory/of/your/files) ; do
perl -i.old -ple "s/syufoft/syhfoft/" $file
done

>> hi this script created the .old file but didnt change the conents of each file.

still remains with syufoft instead of syhfoft

?
OzTEXS
Advisor

Re: change contents in files using grep ?

ok its working now .. just had a typo.

the following script worked like a dream ..


for file in $(ls /directory/of/your/files) ; do
perl -i -ple "s/syufoft/syhfoft/" $file