Need help using sed command in shell script?

Hello,
i want to take the input from user and according to that variable's value search in file emp.lst. Here is what i came up with

echo -e "Enter string to be searched :\c"
read str

sed -n '/\$str/p' emp.lst

this is not working! any idea why?Thanks in advance! :slight_smile:

You should be using "grep" instead of "sed" for this purpose. Anyway, this works for me:

$ cat emp.lst
john reed
peter sullivan
pete blake
rob turner
$ cat em.sh
#!/usr/bin/bash

echo -e "Enter string to be searched :\c"
read str

sed -n "/$str/p" emp.lst
$ em.sh
Enter string to be searched :pete
peter sullivan
pete blake
1 Like

rikxik thanks! :slight_smile:
yeah i should be using grep but my main intention was not to search the emp.lst my main intention of asking this question was how to use variable in sed

you can use a buffer tmp-file

echo "s/${str}/p/" > /tmp/buf_sed_file
sed -f /tmp/buf_sed_file <your text file>

bye

eg

You could have just asked that :slight_smile: