AWK end of line sign in regular expressions

19,037

Solution 1

You can always use:

/\/some_simple_string$/ { print $0 }

I.e. match not only "some_simple_string" but match "/some_simple_string" followed by the end of the line ($ is end of line in regex)

Solution 2

grep '\some_simple_string$' file | tail -n 1 should do the trick.

Or if you really want to use awk do awk '/\/some_simple_string/{x = $0}END{print x}'

Solution 3

To return just the last of a group of matches, ...

Store the line in a variable and print it in the END block.

/some_simple_string/ { x = $0 }
END{ print x }

Solution 4

To print all the matches that end with the string /some_simple_string using regular expression you need to anchor to the the end of the line using $. The most suitable tool for this job is grep:

$ grep '/some_simple_string$' file

In awk the command is much the same:

$ awk '/[/]some_simple_string$/' file

To print all lines after the matching you would do:

$ awk 'print_flag{print;f=0} /[/]some_simple_string$/{print_flag=1}' file

Or just combine grep and tail if it makes it clearer using context option -A to print the following lines:

$ grep -A1 '/some_simple_string$' file | tail -n 1
Share:
19,037

Related videos on Youtube

Mikhail Kalashnikov
Author by

Mikhail Kalashnikov

Software Engineer with solid engineering background and wide range of skills. Areas of expertise: Operating Systems, Reverse Engineering, Web Development Languages: Javascript, C, Python

Updated on June 29, 2022

Comments

  • Mikhail Kalashnikov
    Mikhail Kalashnikov almost 2 years

    I have a simple awk script named "script.awk" that contains:

    /\/some_simple_string/ { print $0;}
    

    I'm using it to parse some file that contains: (by using: cat file | awk -f script.awk)

    14 catcat one_two/some_thing
    15 catcat one_three/one_more_some_simple_string
    16 dogdog one_two/some_simple_string_again
    17 dogdog one_four/some_simple_string
    18 qweqwe firefire/ppp
    

    I want the script to only print the stroke that fully reflect "/some_simple_string[END_OF_LINE]" but not 2 or 3. Is there any simple way to do it?

    I think, the most appropriate way is to add end-of-line sigh to the regular expression. So it will parse only strokes that starting with "/some.." and have a new line at the end of "..string[END_OF_LINE]"

    Desired output:

    17 dogdog one_four/some_simple_string
    

    Sorry for confusion, I was asking for END OF LINE sign in regular expressions. The correct answer is:

    /\/some_simple_string$/ { print $0;}
    
  • lc2817
    lc2817 over 10 years
    why not END{print} using the fact that $0 is set at each line and that print implicitly prints $0?
  • luser droog
    luser droog over 10 years
    I forgot about that. Haven't really used awk in a while. :)
  • Chris Seymour
    Chris Seymour over 10 years
    Both of these just print the last line the file not the last line matching the regular expression
  • Mikhail Kalashnikov
    Mikhail Kalashnikov over 10 years
    That is probably what I've been looking for. I used to use: \\<some_simple_string\>\, but unfortunately it works only in gawk but not in awk.
  • Chris Seymour
    Chris Seymour over 10 years
    @MikhailKalashnikov this answer won't print the last match only, I thought that is what you wanted? Also this will work with any POSIX compliant awk not just gawk.
  • Mikhail Kalashnikov
    Mikhail Kalashnikov over 10 years
    Unfortunately, It didn't work in awk v1.6, and I wanted it to be as portable as possible.
  • Jotne
    Jotne over 10 years
    END {print} is NOT a good solution. It will print any last line, why do the search then?? File may contain other string at last line than some_simple_string. Do use END{ print x }
  • luser droog
    luser droog over 10 years
    @sudo_O Thanks! Twice!
  • Chris Seymour
    Chris Seymour over 10 years
    @luserdroog it seems this is not what the OP wants after all.
  • starbeamrainbowlabs
    starbeamrainbowlabs about 6 years
    Warning: If your file has Windows line endings (\r\n), this might not work as expected. I suggest adding a \s* or similar (it depends on your dataset of course) to the end before the $.