3

I have a long text file in ascii. Every so often it will have Page: ##### in a line of it. I would like to match starting at "Page: 25141" and every single line after that to the end of the document.

Some of the combos I have tried are:

grep -E ^["Page: 25141".*] document.txt
grep  "Page: 25141.*" document.txt
grep  "Page: 25141\.\*" document.txt
grep -E "Page: 25141"[.*] document.txt
grep -E "Page: 25141"{.*} document.txt
grep -E {"Page: 25141".*} document.txt

Can't get this to work.

Zombo
  • 1
  • 62
  • 391
  • 407
dman
  • 10,406
  • 18
  • 102
  • 201

3 Answers3

6

If sed solution is ok for you:

sed -n '/Page: 25141/,$p' file

The above sed will match all lines between the range starting from the line containing the pattern 'Page 25141, till the end of the file($).

Guru
  • 16,456
  • 2
  • 33
  • 46
  • This will do. Like to learn how it is does with grep using .* to end of document. – dman Apr 19 '13 at 04:29
  • .* is a regex which we use to match a pattern within a line. your requirement is not for a line, its till end of file.. – Guru Apr 19 '13 at 04:31
3

Since grep is line-oriented matching multiple lines with a regular expression is not easy. However, the -A option will print the context following any matches:

grep -A 1000000000 'Page: 25141'

You could also do it with ed or sed:

echo '/Page: 25141/,$p' | ed -s filename

I almost got it to work with .*, based on nenopera's answer to a similar question. The only problem is that it prints an extra newline at the end:

grep -Pzo '(?s)Page: 25141.*'
Community
  • 1
  • 1
tom
  • 21,844
  • 6
  • 43
  • 36
0

You can give a thought to awk, which is built for such jobs. awk 'BEGIN{i=0;} /pattern/ {i=1;} {if(i==1) print}' file will do you job nicely, and if you want to do more.

abasu
  • 2,454
  • 19
  • 22