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

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

    I was trying to do this and came across this link: https://www.unix.com/shell-programming-and-scripting/153101-print-next-word-after-found-pattern.html

    Summary: use grep to find matching lines, then use awk to find the pattern and print the next field:

    grep pattern logfile | \
      awk '{for(i=1; i<=NF; i++) if($i~/pattern/) print $(i+1)}'
    

    If you want to know the unique occurrences:

    grep pattern logfile | \
      awk '{for(i=1; i<=NF; i++) if($i~/pattern/) print $(i+1)}' | \
      sort | \
      uniq -c
    

提交回复
热议问题