Use awk to get value of the parameter in the array

3

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
bash
awk
grep
journal
asked on Stack Overflow Jun 29, 2018 by kk_pl • edited Jun 29, 2018 by kk_pl

2 Answers

2

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
answered on Stack Overflow Jun 29, 2018 by Idriss Neumann • edited Jun 29, 2018 by Idriss Neumann
2

This line will give your the value if the key is ccc:

journalctl -b|grep -oP 'ccc: \K[^ :]*'

e.g.

kent$  grep -oP 'ccc: \K[^: ]*'<<<"aaa: 0x00000111 bbb: 0x00000222 ccc: 0x00000333 ddd: adfa"
0x00000333
answered on Stack Overflow Jun 29, 2018 by Kent • edited Jun 29, 2018 by Kent

User contributions licensed under CC BY-SA 3.0