用grep awk提取字符串

 如下日志文本,想要从中提取 androidboot.serialno= 后面的字符串

Feb 19 03:20:15 firefly kernel: [    0.000000] Booting Linux on physical CPU 0x0
Feb 19 03:20:15 firefly kernel: [    0.000000] Initializing cgroup subsys cpuset
Feb 19 03:20:15 firefly kernel: [    0.000000] Initializing cgroup subsys cpu
Feb 19 03:20:15 firefly kernel: [    0.000000] Initializing cgroup subsys cpuacct
Feb 19 03:20:15 firefly kernel: [    0.000000] Linux version 4.4.194 (jincheng@jincheng-PC) (gcc version 6.3.1 20170404 (Linaro GCC 6.3-2017.05) ) #15 SMP Thu Dec 30 14:52:16 CST 2021
Feb 19 03:20:15 firefly kernel: [    0.000000] Boot CPU: AArch64 Processor [410fd034]
Feb 19 03:20:15 firefly kernel: [    0.000000] earlycon: Early serial console at MMIO32 0xff1a0000 (options '')
Feb 19 03:20:15 firefly kernel: [    0.000000] bootconsole [uart0] enabled
Feb 19 03:20:15 firefly kernel: [    0.000000] On node 0 totalpages: 516096
Feb 19 03:20:15 firefly kernel: [    0.000000]   DMA zone: 8184 pages used for memmap
Feb 19 03:20:15 firefly kernel: [    0.000000]   DMA zone: 0 pages reserved
Feb 19 03:20:15 firefly kernel: [    0.000000]   DMA zone: 516096 pages, LIFO batch:31
Feb 19 03:20:15 firefly kernel: [    0.000000] psci: probing for conduit method from DT.
Feb 19 03:20:15 firefly kernel: [    0.000000] psci: PSCIv1.0 detected in firmware.
Feb 19 03:20:15 firefly kernel: [    0.000000] psci: Using standard PSCI v0.2 function IDs
Feb 19 03:20:15 firefly kernel: [    0.000000] psci: Trusted OS migration not required
Feb 19 03:20:15 firefly kernel: [    0.000000] PERCPU: Embedded 21 pages/cpu @ffffffc07fede000 s46312 r8192 d31512 u86016
Feb 19 03:20:15 firefly kernel: [    0.000000] pcpu-alloc: s46312 r8192 d31512 u86016 alloc=21*4096
Feb 19 03:20:15 firefly kernel: [    0.000000] pcpu-alloc: [0] 0 [0] 1 [0] 2 [0] 3 [0] 4 [0] 5
Feb 19 03:20:15 firefly kernel: [    0.000000] Detected VIPT I-cache on CPU0
Feb 19 03:20:15 firefly kernel: [    0.000000] CPU features: enabling workaround for ARM erratum 845719
Feb 19 03:20:15 firefly kernel: [    0.000000] Built 1 zonelists in Zone order, mobility grouping on.  Total pages: 507912
Feb 19 03:20:15 firefly kernel: [    0.000000] Kernel command line: storagemedia=emmc androidboot.storagemedia=emmc androidboot.mode=normal  androidboot.verifiedbootstate=orange androidboot.slot_suffix= androidboot.serialno=0QLQUXNZHZ  ro rootwait earlycon=uart8250,mmio32,0xff1a0000 swiotlb=1 console=ttyFIQ0 root=PARTLABEL=rootfs rootfstype=ext4 overlayroot=device:dev=PARTLABEL=userdata,fstype=ext4,mkfs=1 coherent_pool=1m systemd.gpt_auto=0 cgroup_enable=memory swapaccount=1

可以使用如下方法:

1. 管道方式处理

grep "androidboot.serialno" /var/log/kern.log | xargs -n 1 | grep "androidboot.serialno" | xargs -d = | awk '{print $2}'

2. grep的-P参数使用perl正则语法 

grep -oP 'androidboot.serialno=\K.*?(?= )' /var/log/kern.log
grep -oP 'androidboot.serialno=\K[^ ]+' /var/log/kern.log

3. awk查找 

awk -F'androidboot.serialno=' '{for(i=2; i<=NF;i++){split($i, a, " "); print a[1]}}' /var/log/kern.log

你可能感兴趣的:(linux,运维,服务器)