Simple sed script question

Script newbie, so I'm sure I'm missing something obvious here, but how come this simple script does not work?

#!/bin/bash                                                                                                                                                                                                                                      
sed '/$1/,/^$/!d' $2

I make the script executable and run ./script.sh string filename, but nothing happens and I any result at all, even though this works fine in the shell:

sed '/string/,/^$/!d' filename

use double-quotes instead of the single-quotes - you need to have your script parameter evaluated.

1 Like

Thank you for the quick reply, and yes, that did the trick. Much appreciated. :slight_smile:

---------- Post updated at 06:33 PM ---------- Previous update was at 06:00 PM ----------

And if I want to be able to input multiple filenames? This apparently doesn't work the way I expected:

#!/bin/bash                                                                                                                                                                                                                                      
for i
do
sed "/$1/,/^$/!d" $2
done

./script.sh string filename*

I have 3 files with the name filename1, filename2 and filename3, but my script only returns the result from filename1, and does it three times. I'm lost...

Try:

#!/bin/bash
str=$1
shift
for i
do
 sed '/'"$str"'/,/^$/!d' "$i"
done