tags:

views:

33

answers:

3

I want to get the lines from a file from the line which contains the last occurance of a given word.

Ex:

True, there have been some
changes in the plot. In the original,
Kane tried to buy high political
office for himself. In the new version,
he just puts politicians on his payroll.

If I give "In" then I need

office for himself. In the new version,
he just puts politicians on his payroll.

+1  A: 

Try:

grep 'yourWord' yourFile.txt | tail -n1

Or with sed:

sed -n '/yourWord/{$p}' yourFile.txt
eumiro
Actually i need the whole text which is under the line where the pattern matches @ last
Vijay Athreyan
A: 
$ word="In"
$ awk -vw="$word" '{s=s$0}END{ m=split(s,str,w); print w str[m]}' file
In the new version, he just puts politicians on his payroll.

I don't understand why there is "office for himself." though.

ghostdog74
Its Just a format mistake. Sorry. Thanks for the responce...
Vijay Athreyan
A: 

This should work:

As a one-liner:

patt=In
sed -nr "/$patt/!b;:a;\$!N;/\n.*$patt/{h;s/\n[^\n]*\$//;g;s/^.*\n//};\$!ba;p" inputfile

If your sed requires -e:

patt=In
sed -nr -e "/$patt/!b" -e ":a" -e "\$!N" -e "/\n.*$patt/{h" -e "s/\n[^\n]*\$//" -e "g" -e "s/^.*\n//}" -e "\$!ba" -e "p" inputfile

On separate lines:

patt=In
sed -nr "
    /$patt/!b
    :a
    \$!N
    /\n.*$patt/{
    h
    s/\n[^\n]*\$//
    g
    s/^.*\n//
    }
    \$!ba
    p' inputfile
Dennis Williamson