2

I am trying to write a script that copies the file log_REPLACE, and replaces the string REPLACE in the name of the file, and within the file with the letter A and B.

So I want to create log_A, and log_b, and the string REPLACE inside them is replaces with the letter A and B in them respectively.

I'm trying:

cp log_REPLACE log_A
cp log_REPLACE log_B
grep -rl REPLACE  | xargs sed -i 's/REPLACE/A/g'

but that end up rewriting in both copies (*A and *B). is there a better way to do this?

1 Answer 1

2

You can loop over the possible filename substrings, make the replacements with sed, save the STDOUT from sed as the desired filename:

for i in a b; do
    sed "s/REPLACE/$i/g" log_REPLACE >"log_$i"
done

Make necessary changes to meet your need.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .