用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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值