English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Here's what i have so far...
I created a new script, subst2, using three parameters,

1. the string to be replaced ($1)
2. the string with which to replace it ( $2)
3.the name of the file in which to make the substitution ($3)

This performed the substitution and backup file creation when the target string occurs somewhere in the file, but leaves the file completely unchanged if the string being replaced is nowhere in the file.

I got this
if grep "$1" "$3" > /dev/null; then
mv $3 $3.bak
sed "s/$1/$2/g" $3.bak > $3
# grep returned 0, the pattern occurred in at least one line
fi

I got that right but I have a problem with this part right here..

Generalize your subst2 script, producing a new script subst that will apply a substitution to any number of files given on the command line. For example

~/Unix/script/subst foo bar myFile1.txt myFile2.txt myFile3.txt

should apply the same substitution throughout each of the 3 files named there.

I have this code in bash but any code is fine.

2007-08-08 15:32:52 · 1 answers · asked by ddw4e 2 in Computers & Internet Programming & Design

1 answers

You'll do something like:

# Peel off the first two command-line arguments
PAT=$1
shift
REPL=$1
shift

# Loop through the rest
for FILE in $*
do
[your original script, replace
from $PAT to $REPL, for $FILE]
done

2007-08-08 15:42:55 · answer #1 · answered by McFate 7 · 1 0

fedest.com, questions and answers