I'm trying to make a simple bash script to take user input and then search a .lst file for lines matching said input and then the count of matching lines. I'm pretty inexperienced with bash so I was wondering if anyone could point out what I'm doing incorrectly. Here's what I've got going on:

 echo "Enter pattern to be searched: "
 read pattern

 sed -n "/$pattern/p" "$file"
 sed -n "/$pattern/p" "$file" > file.lst

 echo "number of matching lines: "
 cat file.lst | wc -l

This returns matching lines when I write the specific filename in the script, but it doesn't work when I pass the filename as an argument and invoke it like

./script.sh myfile.lst

What am I doing incorrectly? Thanks ! I'm trying to find a solution that doesn't use awk or grep


1 Answer 1


I figured it out lol, was making it needlessly complicated. The solution was to write

sed -n '/$pattern/p' $1

You must log in to answer this question.

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