I have written the following shell to count the number of lines starting with the pattern of " A valA B valB". However, I think that I have not passed variables properly. Any help to fix that?
for i in {0..16};
do
for j in {0..16};
do
echo A $i B $j
grep '^ A : "$i" B : "$j"' file | wc -l
done
done
You can use the grep command or egerp command for any given input files, selecting lines that match one or more patterns. By default the output shown on screen. But, you can store output to variable in your shell scripts.
To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'. By default, grep searches for a pattern in a case-sensitive way.
Use proper bash quoting. Variables are not expanded inside ''
. See the link for reference.
grep "^ A : $i B : $j" file | wc -l
Also perhaps you mean this, but just try either.
grep "^ A : \"$i\" B : \"$j\"" file | wc -l
wc -l
, you can directly use grep -c
for counting the matchesYou can use:
grep -c "^ A : $i B : $j" file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With