I have a file like this:
string log 1
string log 2
string match
string log 4
string match
string log 5
string log 6
I need to get all the lines after the last string match. How can I do it in bash?
p - Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option. n - If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input.
The -n ( or --line-number ) option tells grep to show the line number of the lines containing a string that matches a pattern. When this option is used, grep prints the matches to standard output prefixed with the line number.
First, find the last string match:
line=$(grep -n 'string match' myFile | cut -d: -f1 | tail -1)
Then, print all lines up to that last string match:
sed -n "1,${line}p" myFile
If you need all lines after last match:
sed -n "$((line+1))"',$p' myFile
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