bashawkgrepjournal

Use awk to get value of the parameter in the array


journalctl -b showed me log like this:

something something somethng
aaa: 0x00000111 bbb: 0x00000222 ccc: 0x00000333 ddd: 0x00000444
something something somethng

How to get value of parameter 'ccc'?

For example:

journalctl -b | awk '/ccc:/{print $1}'

showed first word, but I need to get first word after 'ccc':

0x00000333

Solution

  • Using sed:

    $ echo "aaa: 0x00000111 bbb: 0x00000222 ccc: 0x00000333 ddd: 0x00000444"|sed 's/.*ccc: \([^ ]*\).*/\1/g'
    0x00000333
    

    If you really want to use awk:

    $ echo "aaa: 0x00000111 bbb: 0x00000222 ccc: 0x00000333 ddd: 0x00000444"|awk '/ccc:/{s=$0; gsub(".*ccc: ", "", s); gsub(" .*$", "", s); print s}'
    0x00000333