1833406 Members
2748 Online
110052 Solutions
New Discussion

Re: script help

 
SOLVED
Go to solution
Michael_33
Regular Advisor

script help

Hi all,

There are many .txt files in /usr directory,
each file has a line with this content: 1992-2002, I want to change it to 1992-2003 by one time, how should I do? thanks.

5 REPLIES 5
Wilfred Chau_1
Respected Contributor
Solution

Re: script help

either vi each file, then
type ":0,$s/1992-2002/1992-2003/g

or

cp a.txt a.txt.save
sed -e "s/1992-2002/1992-2003/" a.txt.save > a.txt

Jeff Schussele
Honored Contributor

Re: script help

Hi Michael,

Assuming all .txt files in the dir need the change you can use the following sed script

for x in `ls /usr/*.txt`
>do
>sed 's/"1992-2002"/"1992-2003"/g' $x > $x.new
>done

The g (global) is required to catch all instances in the file. Without it only the first occurence gets the change

If you don't wish to keep the old copies add this after the sed command
>mv $x.new $x

HTH,
Jeff

PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
S.K. Chan
Honored Contributor

Re: script help

Assumption made .. all of your text files are in /usr level.
# cd /usr
# for i in $(ls *.txt)
> do
> sed 's/1992-2002/1992-2003/g' $i > $i.new
> mv $i.new $i
> done
#
That should do it. Test it first by copying a few *.txt files in /tmp and run it from there.
Michael_33
Regular Advisor

Re: script help

thanks all!
H.Merijn Brand (procura
Honored Contributor

Re: script help

Just for the record, perl can do it in a single op with in-file editing:

# perl -pi -e's/1992-2002/1992-2003/g' *.txt
Enjoy, Have FUN! H.Merijn