zoukankan      html  css  js  c++  java
  • use AWK to extract some lines according to some patterns in file

    A file named  Demo.txt, is like this:

    @@@0x5100002c is 0 @@@
    0x51000018 is 101111
    0x5100001c is e000e
    0x51000020 is 121111
    0x51000024 is f000f
    0x51000024 is f000f
    0x51000028 is 0
    0x51000030=0x00000606
    0x51000034=0x00445221
    0x51000038=0x02003faa
    
    dly90_para_lock=0xf000f
    max_begin is 0
    max_end is 23
    max_one is 24
    dqs_window is 11
    
     r0= 0 , r1 = 1, r2 = 1, r3 = 1, r4 = 1, r5 = 0
    
    SDRDDR_DQSWIN is 22
    SDRDDR_MODE is 100
    DDR Success 1
    DDR Success 2
    DDR Success 3
    DDR Success 4
    @@@0x5100002c is 26260006 @@@
    After ddr_init!
    0x51000018=0x00101111
    0x5100001c=0x000e000e
    0x51000020=0x00121111
    0x51000024=0x000f000f
    0x51000028 =0x00000000
    0x51000030=0x00000606
    0x51000034=0x00445221
    0x51000038=0x02003faa
    

    Now, I want to extract lines

    dly90_para_lock=0xf000f
    max_begin is 0
    max_end is 23
    

     and the line heading with "@@@0x5100002c"

    So, I use

    [shawn@test]$ cat Demo.txt | awk ' $1 ~ /dly90_para_lock/, $1=="max_end"; $1=="@@@0x5100002c" {print $0} '

    The result is as follows:

    dly90_para_lock=0x100010
    max_begin is 0
    max_end is 24
    @@@0x5100002c is 27270006 @@@
    @@@0x5100002c is 0 @@@
    dly90_para_lock=0xf000f
    max_begin is 0
    max_end is 23
    @@@0x5100002c is 26260006 @@@
    @@@0x5100002c is 0 @@@
    dly90_para_lock=0xf000f
    max_begin is 0
    max_end is 22
    @@@0x5100002c is 27270006 @@@
    @@@0x5100002c is 0 @@@
    dly90_para_lock=0xf000f
    max_begin is 0
    max_end is 22
    @@@0x5100002c is 27270006 @@@

    Parse:

    in awk ' $1 ~ /dly90_para_lock/, $1=="max_end"; $1=="@@@0x5100002c" {print $0} '

    $1 ~ /dly90_para_lock/, $1=="max_end" is a pattern, it means,

    begin from the line which contains substring "dly90_para_lock", and end at the line containing substring "max_end"

    the ';'  is a delimeter for another pattern,

     $1=="@@@0x5100002c" is another pattern, it means,

    if the first column in a line is the string "@@@0x5100002c",

    And the action for both patterns is print the whole line. indicated by  $0

    The two patterns ensure the file is dealt with in order,  so the output is in order.

    cat Demo.txt | awk ' sub(/^dly90_para_lock=/ , "") && sub(/....$/, "") ; $1=="max_begin", $1=="max_end" {print $3}'

    That's all this time.

  • 相关阅读:
    设计模式总结——程序猿武功秘籍(下一个)
    easyui datagrid显示进度条控制操作
    使用CountDownLatch和CyclicBarrier处理并发线程
    人类探索地外文明显著取得的进展
    Linux 启动过程的详细解释
    不会跳回到微博认定申请书
    unix域套接字UDP网络编程
    VS SQL 出现%CommonDir%dte80a.olb 该解决方案
    数据仓库与数据挖掘的一些基本概念
    CheckBoxPreference组件
  • 原文地址:https://www.cnblogs.com/lake-of-embedded-system/p/3274614.html
Copyright © 2011-2022 走看看