0

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.

2
  • What's the file layout?
    – user1931
    May 17, 2010 at 11:23
  • 1
    Hmm, G:/ -- is that Cygwin on Windows?
    – nik
    May 17, 2010 at 11:45

3 Answers 3

2

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

4
  • 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.
    – user3109
    May 17, 2010 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, 2010 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.
    – user3109
    May 17, 2010 at 14:52
  • I'm glad to know it worked for you. And I appreciate the follow up - thank you.
    – JRobert
    May 17, 2010 at 15:55
1

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
4
  • 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, 2010 at 11:48
  • I had to do sed "s/\(.*\)\(G:.*\)/\2/"
    – user3109
    May 17, 2010 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). May 17, 2010 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.
    – user3109
    May 17, 2010 at 14:53
1

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.
    

You must log in to answer this question.