Using grep to get the next WORD after a match in each line

前端 未结 6 1337
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 07:09

I want to get the \"GET\" queries from my server logs.

For example, this is the server log

1.0.0.127.in-addr.arpa - - [10/Jun/2012          


        
6条回答
  •  忘掉有多难
    2020-11-30 07:31

    Assuming you have gnu grep, you can use perl-style regex to do a positive lookbehind:

    grep -oP '(?<=GET\s/)\w+' file
    

    If you don't have gnu grep, then I'd advise just using sed:

    sed -n '/^.*GET[[:space:]]\{1,\}\/\([-_[:alnum:]]\{1,\}\).*$/s//\1/p' file
    

    If you happen to have gnu sed, that can be greatly simplified:

    sed -n '/^.*GET\s\+\/\(\w\+\).*$/s//\1/p' file
    

    The bottom line here is, you certainly don't need pipes to accomplish this. grep or sed alone will suffice.

提交回复
热议问题