1846676 Members
3287 Online
110256 Solutions
New Discussion

Re: edit script

 
SOLVED
Go to solution
Peter Gillis
Super Advisor

edit script

Hi ... ux 11.11v1 op sys, posix sh.
I need to change a heap of lines in a heap of scripts the changes are directory names.
eg /opt/CA/sche needs to change to /opt/CA/Umja/sche
I seeem to remember there is a way to make changes like this relatively quickly. Does anyone know of a way?

pleanty points around
thanks Maria
12 REPLIES 12
Steven E. Protter
Exalted Contributor

Re: edit script

Shalom Maria,

The tool of choice is sed.

Here is some sample code.

The essense the sed s/search/replacewith/g command.

So create a file list with fill path. Put it in a file called /tmp/filelist

while read -r $filename
do

sed s/juf.org/juf.net/g ${filename} > ${filename}.bck"
mv ${filename}.bck ${filename}

done < /tmp/filelist


Please test this out before using it in production. The slashes will require a slight upgrade of my basic code.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
RAC_1
Honored Contributor

Re: edit script

Backup all scripts that are to be changed.

sed 's/\opt\/CA\/sche/\/opt\/CA\/Umja\/sche/g' your_scripts
There is no substitute to HARDWORK
H.Merijn Brand (procura
Honored Contributor

Re: edit script

perl supports in-line change

# perl -li -e's{/opt/CA/sche\b}{/opt/CA/Umja/sche}g' all the files you want changed

Or, if you want to change all *files* recursively from a folder/directory

# find dir -type f | xargs perl -li -e's{/opt/CA/sche\b}{/opt/CA/Umja/sche}g'

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Peter Gillis
Super Advisor

Re: edit script

RAC procure...do either of your methods actually change all mention of /opt/CA/sche
to /opt/CA/sche/UM inside all the files in the directory, including if the path name has further directories added on to it?
I am an absolute novice with perl and sed. (that's probably the most helpful piece of info I can give you!)
thanks
Maria.
H.Merijn Brand (procura
Honored Contributor

Re: edit script

In this case, perl is just like sed and awk, it's only capable to in-line change the files, so you do not need temproray files

If you only need to change files in one single dir, and not in subdirs,

# cd that_dir
# perl -li -e's{/opt/CA/sche\b}{/opt/CA/Umja/sche}g' *

will change all files for you. All occurannces.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Peter Gillis
Super Advisor

Re: edit script

Hi,
I have not been able to get result with the perl command..but with the sed command - I been testing on one file only - I issue the command and the actual script that I want the changes to take place in, scrolls past with the necesary test changes. But, then, I issue the more command to view slowly the entire contents of the file and there are NO changes !! What is happening? do you know?
thyanks
Maria
Mel Burslan
Honored Contributor
Solution

Re: edit script

Maria,

sed command itself does not make the changes to the original file. When you run it, it gives you the changed output to the stdout which is your terminal.

assuming you are using RAC's one-liner sed command, you can do this:

cd /my/scripts/directory
for script in `ls`
do
cat ${script} | sed 's/\opt\/CA\/sche/\/opt\/CA\/Umja\/sche/g' > ${script}.new
done

this will generate a new file with .new appended to the original filename in the same directory, which will be the modified version.

Hope this helps
________________________________
UNIX because I majored in cryptology...
spex
Honored Contributor

Re: edit script

Maria,

It sounds as though you're not redirecting the output from sed to a file, so it's being displayed on stdout. Make sure to use this syntax:

sed s/juf.org/juf.net/g file_in > file_out

Then 'more file_out'.

PCS
Marvin Strong
Honored Contributor

Re: edit script

The perl command I would use is

perl -pi -e's|/opt/CA/sche\b|/opt/CA/Umja/sche|g' *

And if you wanted to save an old copy

perl -pi.old -e's|/opt/CA/sche\b|/opt/CA/Umja/sche|g' *
Marvin Strong
Honored Contributor

Re: edit script

oh btw that would be ran from the directory with all the files you want to change. hopefully that was obvious.
H.Merijn Brand (procura
Honored Contributor

Re: edit script

Sorry for the confusion caused by my typoes!

perl -li -e

is wrong

perl -pi -e

is correct.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Peter Gillis
Super Advisor

Re: edit script

Thankyou everyone!!!
Mel, I used your method and it worked well for me. The only thing I altered was number of slashes in the command:
sed 's/\opt\/CA\/sche/\opt\/CA\/UnicenterUUJMA\/sche/g' maria
I have learnt plenty tonight, thankyou.
Maria