Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I forgot a lot of my command line. I am doing cat file | grep "error" and i would like it to show everything to the right of G:/ including G:/ if possible. I figure its an awk command but i dont know what. I tried awk '{print $8+}' but + does not work like i hoped and guessed.

share|improve this question
    
What's the file layout? – John T May 17 '10 at 11:23
1  
Hmm, G:/ -- is that Cygwin on Windows? – nik May 17 '10 at 11:45
up vote 3 down vote accepted

awk '/G:/{ print substr($0, match($0, /G:/)); }' file

share|improve this answer
    
That prints the full line. Also i can see you havent tested as there is two ) which gives it an error. The sed solution above works. – acidzombie24 May 17 '10 at 11:58
    
Did you run it? I did. It works just fine based on based on your description of the file (which I understood to be "stuff...G:/more stuff more stuff"). The two parens are required; one to close match(), one to close substr(). match() finds the string you wanted to print; substr() prints from where that string begins to the end of the line. Also: you're welcome. – JRobert May 17 '10 at 12:48
    
Oh sorry. I did run it but i did it wrong. Which explains why i had the syntax error. oops, sorry. This works great. – acidzombie24 May 17 '10 at 14:52
    
I'm glad to know it worked for you. And I appreciate the follow up - thank you. – JRobert May 17 '10 at 15:55

Try:

cat file | sed "s/.*\(G:\/\)/\1/"

That will remove everything before G:/. Be aware that if you have multiple G:/ entries it will match the very last entry. If you're only working with a single file instead do:

sed "s/.*\(G:\/\)/\1/" file
share|improve this answer
    
Wouldn't you want to catch the text after the G:/ to the end of line? You are missing a couple of chars in the pattern... However, the leading .* in the pattern is probably not going to let it work as expected even with that fix. – nik May 17 '10 at 11:48
    
I had to do sed "s/\(.*\)\(G:.*\)/\2/" – acidzombie24 May 17 '10 at 11:55
    
@acidzombie24: You probably don't need to capture the first group. This will probably work: s/.*\(G:.*\)/\1/ (the parentheses need to be escaped unless you are using the -r option or your version of sed does that for you). – Dennis Williamson May 17 '10 at 13:50
    
I copied the results here wrong. What happened on my side was since the first group wasnt capture they werent getting replaced/they were still showing up in stdout. – acidzombie24 May 17 '10 at 14:53

You can just do:

  grep error file | grep -oP 'G:.+'

Notes:

  1. You do not need to cat the file, just directly grep 'error' in it.
  2. The second grep uses the P flag for perl regular expressions. The regex says: Find 'G:' and any characters following.
  3. The o flag makes grep print the matched string only:

    -o, --only-matching
          Print  only  the  matched  (non-empty) parts of a matching line,
          with each such part on a separate output line.
    
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .