bash search for string in each line of file bash search for string in each line of file bash bash

bash search for string in each line of file


Don't use bash, use grep.

grep -F "$cnty" wheatvrice.csv >> wr_imp.csv


While I would suggest to simply use grep too, the question is open, why you approach didn't work. Here a self referential modification of your second approach - with keyword 'bash' to match itself:

#!/bin/bashcnty=bash    while read -r linedo    case $line in         *${cnty}*)             echo $line " yes" >> bashgrep.log            ;;        *)                echo "no"                ;;    esacdone < bashgrep.sh

The keypoint is while read -r line ... < FILE. Your command with cat involves String splitting, so every single word is processed in the loop, not every line.

The same problem in example 1.