I am reviewing my bash knowledge and I stuck on the following: I want to count the number of the last few lines that match the string 'Wed' which is case insensitive in one csv file. I do not need the content of the line though.
INPUT="$1"
IFS=","
last_rows=$(tail -n 10 $INPUT)
echo $(grep -c 'Wed' $last_rows)
output:
grep: Wednesday: No such file or directory
but this gives me grep: (the content of the line): no such file or directory Can anyone please give me any hints? Thanks in advance!
CodePudding user response:
To grep inside a string, you do a
grep -c Wed <<<$last_rows
Of course, if you don't need the content of last_rows in a different place of your script, there is no point in using a variable. Instead, you just do a
tail -n 10 "$INPUT" | grep -c Wed
Don't forget to quote $INPUT, otherwise the code will fail if the filename contains a space.
CodePudding user response:
Try:
tail -n 10 "$INPUT" | grep -i -c wed
The option -i makes the search in grep case insensitive.
