-1

I'm trying to use a shell script to get the string from one file, then use it to get the matched sentence.

The script looks like this:

function find_str (){
    echo $1
    grep -e "\/$1" info.txt
}

for word in $(<./name.TXT); do
   #egrep -w "$word" info.txt  #this can't work either
   find_str $word
done 

It turns out that find_str $word cannot match some string like "/WORD1 balabala"

Any suggestion about this short piece of script?

kvantour
  • 25,269
  • 4
  • 47
  • 72

1 Answers1

0

What is the "/$1" part about? Are you looking for literal slashes? And do you need the function?

$ cat > info.txt
testing 123 dog
nothing matches a catalyst
doggerel is not poetry
my cat is a maine coone

$ cat > name.txt
dog cat

With -w and gnu grep, only lines with matching whole words are listed:

$ for word in $(<name.txt); do grep -w "$word" info.txt; done
testing 123 dog
my cat is a maine coone

Without the -w flag, all lines containing a match are listed:

$ for word in $(<name.txt); do grep "$word" info.txt; done
testing 123 dog
doggerel is not poetry
nothing matches a catalyst
my cat is a maine coone
Ian McGowan
  • 3,461
  • 3
  • 18
  • 23