Score:1

Bash script to return lines matching a pattern and then count them?

ms flag

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

hr flag
Are you asking [How can I get a variable from terminal to use it in my script?](https://askubuntu.com/questions/398357/how-can-i-get-a-variable-from-terminal-to-use-it-in-my-script)
AstralV avatar
ms flag
yes please ! not sure what i'm doing wrong
Score:1
ms flag

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

sed -n '/$pattern/p' $1
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.