Search code examples
unixawkscriptinggrepcut

Extract values from logs


Want to extract the data between the last occurrence of [] in the following logs.

[11/10/14 9:20:57:133 GMT] 000002d2 EmailAdapterE I   Processing Email :: [****][****@hotmail.com][0][1][63][4][4670][0][2014-11-24][12]
[11/10/14 9:20:57:110 GMT] 000002d2 EmailAdapterE I   Processing Email :: [****][****@aol.com][0][1][63][1][3286][0][2014-11-24][10]
[11/10/14 9:20:57:088 GMT] 000002d2 EmailAdapterE I   Processing Email :: [****][*****@pietrawoodandstone.com][1][1][63][1][3455][2000][2014-11-24][26]

From this i want the values between the last [] in the line.

For example from the above line... i want the following output

12
10
26

Tried awk, cut and sed but no success


Solution

  • awk is your friend:

    $ awk -F[][] '{print $(NF-1)}' file
    12
    10
    26
    

    This sets the field separator to either [ or ]. Based on that, the penultimate field contains what you want, so that we print it with $(NF-1).

    Note sed can also make it with some regex:

    $ sed -r 's/.*\[([0-9]*)\]$/\1/' file
    12
    10
    26
    

    This catches the last block of digits contained within [] and prints it back with \1.

    With grep you can also use a look-behind and look-ahead to match what is in between [ and ] + end of line:

    $ grep -Po '(?<=\[)[0-9]*(?=]$)' file
    12
    10
    26