'Count lines below grep and include filename in output
I would like to count the number of lines below a sed command and append filename to the output.
Sample file.txt
Aaaaaaa
Bbbbbbb
Ccccccc
Ddddddd
I would like to grep Bbbbbb and find the number of line below and output the number plus the filename
I tried this cat ${samplename}.txt|sed -n 'Bbbbbbb/,$p'| wc -l but the filename is not in the output
Solution 1:[1]
In order to know the line where "Bbbbb" is found:
grep -n "Bbbbb" file.txt | cut -d ':' -f 1
// grep -n adds line number in front of the search result, this is followed by a colon.
// You get that number by splitting over that colon and take the first field.
In order to know the amount of lines in a file:
wc -l file.txt
In order to perform calculations:
echo $((43 - 7))
Just combine everything :-)
Have fun
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dominique |
