Perl: cut subtitles to end of file

I want to extract a substring between some given word and the end of the file, but I'm out of luck.

This is what my file looks like

non important line a
non important line b
...
...
...
non important line n
keyword
a,b,c,d
e,f,g,h
i,j,k,l

I want to match the keyword with the end of the file:

keyword
a,b,c,d
e,f,g,h
i,j,k,l

I tried this to replace the keyword only with foo, and not everything from the keyword to the end of the file:

cat foo | perl -pi -e 's/keyword.*\Z/foo/g'

Thanks!

+4
source share
2 answers
perl -0777 -i -pe 's/keyword.*\Z/foo/sg'

-0777deletes the entire file at once in $_, otherwise the file will be red in line at $_.

Modifier on the other hand

/s , . \n, $_

+4

s

cat foo | perl -pi -e 's/keyword.*\Z/foo/gs'

http://perldoc.perl.org/perlre.html#Modifiers

+2

All Articles