Home > Software engineering >  How to match a text with newlines after a word using bash
How to match a text with newlines after a word using bash

Time:10-05

I need to match the end of a file after a word match in bash. I have a text like this:

line 1
line 2
line 3
key line
line 5
line 6

I pretend to get everything after "key line", so my output would be:

line 5
line 6

How can I do this using bash? I've tried grep -o -P '(?<=key line\n)[\s\S]*' but it didn't work, although it worked testing in https://regexr.com/

CodePudding user response:

grep is line-based. It doesn't work well when you want to search across lines.

sed is up for the job. This will delete all lines from line 1 to the one containing key line, leaving only the lines after it:

$ sed '1,/key line/d' test.txt
line 5
line 6
  • Related