stephaneeybert
asked on
Unix shell and the sed command...
Dear all,
I have a little shell script to perform a sed on a list of files.
I'm using bash on Linux. The list is correct.
The only problem is that the sed seems not to allow variable arguments.
If instead of the line
cat $i | sed 's/$pattern/$replacement/g ' > $j
I had the line
cat $i | sed 's/mypattern/myreplacement /g' > $j
it would work fine.
So the question is: How to pass variables to a sed command?
Regards
Stephane
#!/bin/sh
# The parameters of the script
pattern=$1
replacement=$2
# A temporary file
tmpfile=/tmp/dump.txt
# Store the search result in the file
# The find prints only the regular files (not the directories)
# The grep prints only the first file occurence without the found line
grep --files-with-matches $pattern `find . -name "*" -type f` > $tmpfile
# The following lines redefine the environment variable
# that contains the characters for word separators
SIFS=$IFS
IFS='
'
echo "Replace $pattern by $replacement"
echo
for line in `cat $tmpfile`
do
# Get the first field of the list
i=`echo $line`
# Replace the string using a temporary file
j=`echo $i'.new'`
# cat $i | sed 's/$pattern/$replacement/g ' > $j
cat $i | sed 's/mypattern/myreplacement /g' > $j
echo "File is : $i"
mv $j "$i"
rm -fr $j
done
# Reset the environment variable
IFS=$SIFS
exit $?
I have a little shell script to perform a sed on a list of files.
I'm using bash on Linux. The list is correct.
The only problem is that the sed seems not to allow variable arguments.
If instead of the line
cat $i | sed 's/$pattern/$replacement/g
I had the line
cat $i | sed 's/mypattern/myreplacement
it would work fine.
So the question is: How to pass variables to a sed command?
Regards
Stephane
#!/bin/sh
# The parameters of the script
pattern=$1
replacement=$2
# A temporary file
tmpfile=/tmp/dump.txt
# Store the search result in the file
# The find prints only the regular files (not the directories)
# The grep prints only the first file occurence without the found line
grep --files-with-matches $pattern `find . -name "*" -type f` > $tmpfile
# The following lines redefine the environment variable
# that contains the characters for word separators
SIFS=$IFS
IFS='
'
echo "Replace $pattern by $replacement"
echo
for line in `cat $tmpfile`
do
# Get the first field of the list
i=`echo $line`
# Replace the string using a temporary file
j=`echo $i'.new'`
# cat $i | sed 's/$pattern/$replacement/g
cat $i | sed 's/mypattern/myreplacement
echo "File is : $i"
mv $j "$i"
rm -fr $j
done
# Reset the environment variable
IFS=$SIFS
exit $?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> Use double-quotes
.. if $pattern and/or $replacement does *not* contain double-quotes
.. if $pattern and/or $replacement does *not* contain double-quotes
ASKER
I will try it!
Cheers
Cheers
Actually, pattern and replacement can contain double-quotes, AND spaces, tabs, or whatever.
In the answer, if the pattern or replacement contains spaces, the command will fail with a garbled sed command.
Of course, this is true for for bourne and korn shells. Other shells, YMMV.
In the answer, if the pattern or replacement contains spaces, the command will fail with a garbled sed command.
Of course, this is true for for bourne and korn shells. Other shells, YMMV.
ASKER
I see.
Thanks for the notification...
Thanks for the notification...
Use double-quotes