Skip to content
Advertisement

AWK with commands [closed]

I’m learning in Linux and my senior give me a task that I find a word in a syslog through AWK command where that all the details of that word showing. I check out google and but there is not a single command I found which is helpful.

Advertisement

Answer

You can use below commands

dmesg | awk ' /pattern/ {print}'

awk '/pattern/ {print}' /var/log/syslog

Some examples.

$ awk '/kernel/ {print}' /var/log/syslog
Nov  5 10:36:01 HardwareTestPC kernel: [135368.195103] codelite-make[29069]: segfault at 0 ip 00007fd7360cca1b sp 00007ffcb98617e0 error 4 in libc-2.27.so[7fd735f99000+1e7000]
Nov  5 10:36:02 HardwareTestPC kernel: [135368.682625] codelite-make[29149]: segfault at 0 ip 00007fd7360cca1b sp 00007ffcb98611d0 error 4 in libc-2.27.so[7fd735f99000+1e7000]
Nov  5 10:40:01 HardwareTestPC kernel: [135608.437387] codelite-make[11438]: segfault at 0 ip 00007f5b9770fa1b sp 00007ffd774678b0 error 4 in libc-2.27.so[7f5b975dc000+1e7000]
Nov  5 10:40:02 HardwareTestPC kernel: [135608.695397] codelite-make[11474]: segfault at 0 ip 00007f5b9770fa1b sp 00007ffd774672a0 error 4 in libc-2.27.so[7f5b975dc000+1e7000]
Nov  5 10:47:59 HardwareTestPC kernel: [136086.395728] codelite-make[7425]: segfault at 0 ip 00007fb8bc720a1b sp 00007ffe65c356c0 error 4 in libc-2.27.so[7fb8bc5ed000+1e7000]
Nov  5 10:48:00 HardwareTestPC kernel: [136086.674097] codelite-make[7460]: segfault at 0 ip 00007fb8bc720a1b sp 00007ffe65c350b0 error 4 in libc-2.27.so[7fb8bc5ed000+1e7000]

You can specify fields to print as well. awk command splits the line into fields (addressable by $1 to $NF) using space as delimiter.

$ awk '/kernel/ {print $2, $3, $4}' /var/log/syslog
5 10:36:01 HardwareTestPC
5 10:36:02 HardwareTestPC
5 10:40:01 HardwareTestPC
5 10:40:02 HardwareTestPC
5 10:47:59 HardwareTestPC
5 10:48:00 HardwareTestPC
5 10:57:13 HardwareTestPC
5 10:59:46 HardwareTestPC
5 11:04:30 HardwareTestPC
5 11:04:30 HardwareTestPC
5 11:04:42 HardwareTestPC
5 11:04:42 HardwareTestPC

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement