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

前端 未结 6 1324
佛祖请我去吃肉
佛祖请我去吃肉 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:21

    It's often easier to use a pipeline rather than a single complex regular expression. This works on the data you provided:

    fgrep GET /tmp/foo | 
        egrep -o 'GET (.*) HTTP' |
        sed -r 's/^GET \/(.+) HTTP/\1/'
    

    This pipeline returns the following results:

    hello
    ss
    

    There are certainly other ways to get the job done, but this patently works on the provided corpus.

提交回复
热议问题