FrenchKheldar
FrenchKheldar

Reputation: 485

Batch delete of multiple lines in multiple files with sed

Let's say I want to delete the same lines in a bunch of files. I have a simple bash script like this:

st=$1
en=$2
shift 2
for i in $*; do
   echo "sed -i -e '${st},${en}d' $i"
   sed -i -e '${st},${en}d' $i
done

which I call like this:

[03:18] ./cleanup.sh 2731 3009 trace_X+0.01080_Y+0.00309_Z-0.00000.dat

and I get

sed -i -e '2731,3009d' trace_X+0.01080_Y+0.00309_Z-0.00000.dat
sed: -e expression #1, char 12: unterminated `s' command

What am I doing wrong? I tried adding more quotes around the variables or the filename in the calling statement but no luck...

Thanks for the help !

Upvotes: 2

Views: 596

Answers (1)

fge
fge

Reputation: 121840

You should use double quotes in your actual sed command:

sed -i -e "${st},${en}d" $i

There is no variable expansion within single quotes.

The fact that it did expand in the echo is because the string you are echoing is surrounded by double quotes.

And if, in fact, these are only ever numbers you are dealing with, you can even drop quotes completely (sed -i -e ${st},${en}d).

Upvotes: 3

Related Questions