1834811 Members
2496 Online
110070 Solutions
New Discussion

Re: awk or sed

 
NS_2
Occasional Contributor

awk or sed

HI masters,
I have number of pro*c and .sql file which contain,
substr(a.st_no,1,3) OR substr(st_no,1,3).

I want to change it to,
substr(a.st_no,1,4) OR substr(st_no,1,4).

How can i do this using awk or sed?
Thanks in adv.
(Pts. will be given to each response.)

Take It Easy, Be Happy !
4 REPLIES 4
Leif Halvarsson_2
Honored Contributor

Re: awk or sed

Hi
I think sed is best for this.


sed 's/substr(a.st_no,1,3)/substr(a.st_no,1,4)/' file1 >file2
S.K. Chan
Honored Contributor

Re: awk or sed

This should work (being very specific here ..)
# sed 's/st_no,1,3/st_no,1,4/g' filename > filename.new
Do a quick for loop for say all files that has ".sql" extension.
# for i in `ls *.sql`
>do
>sed 's/st_no,1,3/st_no,1,4/g' $i > $i.new
>done
#
The *.sql.new files are the ones with the replaced string.
harry d brown jr
Honored Contributor

Re: awk or sed

Nasty, but what the hell:


find PATHWHEREFILESLIVE -type f -name "*.sql" -exec grep -l -e "substr(a.st_no,1,3)" -e "subs
tr(st_no,1,3)" {} \; |
awk '{printf("sed -e \"s/substr\(a.st_no,1,3\)/substr\(a.st_no,1,4\)/\" -e \"s/s
ubstr\(st_no,1,3\)/substr\(st_no,1,4\)/\" %s > %s.dummy; mv %s.dummy %s\n", $1, $1,
$1, $1); }' |xargs -i sh {}



BUT PLEASE make SURE YOU BACKUP your files FIRST!!!!!!!!!

live free or die
harry
Live Free or Die
Tom Maloy
Respected Contributor

Re: awk or sed

Well, just in case you might consider perl...

perl -i -ne 's/st_no,1,3\)/st_no,1,4\)/g;print' yourfile

Note that this (-i option) will edit IN PLACE, overwriting your original files with the changes. To preserve your old files, save a copy, or use:

perl -i.bak -ne 's/st_no,1,3\)/st_no,1,4\)/g;print' yourfile

to keep copies with *.bak name.

Tom
Carpe diem!