Search code examples
awkgrepzshcut

Extract fields from logs with awk and aggregate them for a new command


I have this kind of log:

2018-10-05 09:12:38 286 <190>1 2018-10-05T09:12:38.474640+00:00 app web - - Class uuid=uuid-number-one cp=xxx action='xxxx'
2018-10-05 10:11:23 286 <190>1 2018-10-05T10:11:23.474640+00:00 app web - - Class uuid=uuid-number-two cp=xxx action='xxxx'

I need to extract uuid and run a second query with:

./getlogs --search 'uuid-number-one OR uuid-number-two'

For the moment for the first query I do this to extract uuid:

./getlogs | grep 'uuid' | awk 'BEGIN {FS="="} { print $2 }' | cut -d' ' -f1

My three question :

  • I think I could get rid of grep and cut and use only awk?
  • How could I capture only the value of uuid. I tried awk '/uuid=\S*/{ print $1 }' or awk 'BEGIN {FS="uuid=\\S*"} { print $1 }' but it's a failure.
  • How could I aggregate the result and turn it into one shell variable that I can use after for the new command?

Solution

  • You could define two field separators:

    $ awk -F['= '] '/uuid/{print $12}' file
    

    Result:

    uuid-number-one
    uuid-number-two
    

    Question 2:

    The pattern part in awk just selects lines to process. It doesn't change the internal variables like $1 or NF. You need to do the replacement afterwards:

    $ awk '/uuid=/{print gensub(/.*uuid=(\S*).*/, "\\1", "")}' file
    

    Question 3:

    var=$(awk -F['= '] '/uuid/{r=r","$12}END{print substr(r,2)}' file)
    

    Implement the actual aggregation for each line (here r=r","$12).