37

I have an large textfile that contains an unique string in the middle. What i want to do is to print everything AFTER the string by using grep.

cat textfile | grep "target_string"
This highlights target_string but prints the whole file

cat textfile | grep -o "target_string"
This prints only target_string

cat textfile | grep -o "target_string*"
This prints only target_string

How can i print everything after target_string and nothing before?

djechlin
  • 59,258
  • 35
  • 162
  • 290
twk789
  • 373
  • 1
  • 3
  • 4
  • 5
    By "everything", you mean just everything ON THAT LINE, right? It looks like a lot of people interpreted your question to mean THE REST OF THE FILE. – iconoclast Aug 23 '14 at 02:41
  • I added an answer to print everything after the string ON THAT LINE, and not the rest of the file – Chris Koknat Oct 07 '15 at 14:07

4 Answers4

52

Strangely, the accepted answer printed out the whole line, where I just wanted all the info after the target string. This worked for me:

sed -n 's/target_string//p' filename

Adapted from this post

Community
  • 1
  • 1
chimeric
  • 855
  • 1
  • 9
  • 14
  • 14
    This assumes `target_string` is at the beginning of line. You can change the regex to `.*target_string` to fix that. – tripleee Feb 17 '16 at 15:14
35

With GNU grep, try -B0 -A999999999 or similar. A better choice might be awk:

awk '/target_string/ {seen = 1}
     seen            {print}'

If (your problem specification is slightly unclear) you don't also need to print the matching line, sed is even shorter:

sed '1,/target_string/d'
geekosaur
  • 59,309
  • 11
  • 123
  • 114
  • 1
    The sed line worked perfectly for what I needed, thanks! – dimo414 May 30 '12 at 17:10
  • You can also use `-C 3` to get the lines around the line - I completely forgot about these options! – Wilf Jun 15 '14 at 08:30
  • 7
    This is extremely useful info, and exactly what *I'm* looking for, but I'm pretty sure the OP only wanted to match *the rest of the line*. It's not immediately obvious at first glance, but if you read the question carefully I think this shows through. ("Print everything *on line* after match" instead of "Print everything *after matching line*") Of course it's also possible that this clarification (or obfuscation?) was the result of edits made after you answered... – iconoclast Aug 23 '14 at 02:49
30

You forgot the '.':

    cat textfile | grep -o "target_string.*"
ysdx
  • 8,889
  • 1
  • 38
  • 51
6

This will print everything after each match, on that same line only:

perl -lne 'print $1 if /target_string(.*)/' textfile

This will do the same, except it will also print all subsequent lines:

perl -lne 'if ($found){print} else{if (/target_string(.*)/){print $1; $found++}}' textfile
Chris Koknat
  • 3,305
  • 2
  • 29
  • 30
  • 1
    I wanted to print the rest of a single line after a pattern. And the first one worked well. None of the other answers helped. This was to remove everything before a particular directory in a path. – subin Oct 07 '15 at 10:31