Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace string in multiple files from AIX command line

Status
Not open for further replies.

new2unix

Programmer
Feb 5, 2001
143
US
Hi,

I have multiple files such as the following:

File1-M.txt
File2-M.txt
File3-M.txt
...
etc

In each file, there may be lines containing the following text:

data1 = tables/text1
data2 = tables/text2
data3 = /text3

Instead of using vi or other text editor to open each file and manually add "/usr/" to all the "tables/" string, is there a way from the AIX command line to basically go through each file, looking for the string, "tables/" and replace the string with "/usr/tables/"?

Thanks

Mike
 
Yep - Sed'll do it
for filex in File*M.txt
do
sed 's/tables\//\/usr\/tables\//g' > tempfile
mv tempfile $filex
done

HTH ((;-))) Dickie Bird
Honi soit qui mal y pense
 
i agree dickiebird
but be prudent ...
(a bad writted) sed writes empty files :(
before
mv tempfile $filex
put an:
[ -s tempfile ] || continue -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Thanks, Dickiebird. I will give it a try from the command line.

Jamisar, I am not sure about the [ -s tempfile ] || continue syntax. Can you elaborate?

Thanks again,

Mike
 
well... you've forgottento specify 'filex' for your sed solution.

sed 's/tables\//\/usr\/tables\//g' ${filex} > tempfile

If you want to di it in-place, here's the "ex" solution:

for filex in File*M.txt
do
ex - ${filex} <<EOF
%s/tables\//\/usr\/tables\//g
wq!
EOF
done vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top