Operating System - HP-UX
1753546 Members
5569 Online
108795 Solutions
New Discussion юеВ

how to replace strings in multiple files thru script or command line

 
Sachin_29
Advisor

how to replace strings in multiple files thru script or command line

Hi:
I have "n" files in a directory and I want to search for A/B/C/D and replace it with E/F/G/H/I in all the files thru a script or on a command line. I have to do this on 6 servers so ..

any suggestions
5 REPLIES 5
Sundar_7
Honored Contributor

Re: how to replace strings in multiple files thru script or command line

# cd /dir
# for FILE in *
do
mv $FILE $FILE.tmp
sed 's|A/B/C/D|E/F/G/H/I|' $FILE.tmp > $FILE
[ $? -eq 0 ] && rm $FILE.tmp
done

Learn What to do ,How to do and more importantly When to do ?
A. Clay Stephenson
Acclaimed Contributor

Re: how to replace strings in multiple files thru script or command line

I think I would leverage sed to do this although Perl would also be a good choice.

1) Create a file, /var/tmp/x.sed, and insert your replacement instructions.

I'll use '+' as the delimiter because that will make your "/" replacements easier.

s+A/B/C/D+E/F/G/H/I+g

You can put as many sed commands in this file as your like; they will be processed sequentially on each file.

2) Create a script to find the text files in the current directory and sed them.


#!/usr/bin/sh

TDIR=${TMPDIR:-/var/tmp}
TFILE=${TDIR}/X${$}_1.txt
SEDFILE=/var/tmp/x.sed

STAT=0
ls | while read X
do
file "${X}" | grep -q -i "text"
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
sed -f ${SEDFILE} "${X}" > ${TFILE}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
mv ${TFILE} "${X}"
fi
fi
done
exit ${STAT}



If it ain't broke, I can fix that.
Rodney Hills
Honored Contributor

Re: how to replace strings in multiple files thru script or command line

A one line perl command would be:

perl -p -i -e 's{A/B/C/D}{E/F/G/H/I}g' *

If you have openssh installed, you could run it on each of the servers by:

for x in server1 server2 server3 ; do
ssh $x "cd /mydir ; perl -p -i -e 's{A/B/C/D}{E/F/G/H/I}g' *"
done

HTH

-- Rod Hills
There be dragons...
Petr Simik_1
Valued Contributor

Re: how to replace strings in multiple files thru script or command line

This is simple change of string on specific file on multiple servers.

for i in `cat serverlist`
do
remsh $i "sed 's/stringA/stringB/g' /dir/fileX"
done

Muthukumar_5
Honored Contributor

Re: how to replace strings in multiple files thru script or command line

IF you want to change the pattern with in one directory then grep and sed will do that, if you want to do it multiple directories we have to use find,grep and sed commands

Limitation: If we use grep to grep a pattern on a object file ,it will make a lot of error and unreadable symbols.

In a single directory:
find . -name "*" -exec grep -q 'A/B/C/D' {} \; -print | awk '{ print "echo \"`sed -e 's%A/B/C/D%E/F/G/H%' "$1"`\"> "$1 }' | sh

Multiple directory:

Change find . to your testing directory. It will do this in their subdirectories too.

Use -type f instead of -name "*" also
Easy to suggest when don't know about the problem!