Awk print matched column if exists else print not found

后端 未结 3 1943
终归单人心
终归单人心 2021-01-24 03:47

My text file looks like below

date=\"2017-10-10\" ip=192.168.1.1:22 inbound=100 outbound=100
date=\"2017-10-10\" ip=192.168.1.1:22 inbound=100
date=\"2017-10-10         


        
相关标签:
3条回答
  • 2021-01-24 04:11

    Using GNU awk

    awk '{print match($0,/inbound=([0-9]+)/,a)?a[1]:0}' file
    

    In perl

    perl -lne 'print /inbound=(\d+)/?$1:0' file
    

    In sed

    sed 's/.*inbound=\([0-9]\+\).*/\1/;t;s/.*/0/' file
    
    0 讨论(0)
  • 2021-01-24 04:12

    Whenever you have name=value pairs in your input it's best to first create an array of those mappings (f[] below) and then you can just print (or do anything else with) the values by name:

    $ awk -v n="inbound" -F'[ =]+' '{delete f; for (i=1;i<NF;i+=2) f[$i]=$(i+1); print (n in f ? f[n] : "0 Not Found")}' file
    100
    100
    0 Not Found
    

    Want to do the same for "outbound" or any other field? Just init the name variable n accordingly"

    $ awk -v n="outbound" -F'[ =]+' '{delete f; for (i=1;i<NF;i+=2) f[$i]=$(i+1); print (n in f ? f[n] : "0 Not Found")}' file
    100
    0 Not Found
    100
    $
    $ awk -v n="date" -F'[ =]+' '{delete f; for (i=1;i<NF;i+=2) f[$i]=$(i+1); print (n in f ? f[n] : "0 Not Found")}' file
    "2017-10-10"
    "2017-10-10"
    "2017-10-10"
    $
    $ awk -v n="ip" -F'[ =]+' '{delete f; for (i=1;i<NF;i+=2) f[$i]=$(i+1); print (n in f ? f[n] : "0 Not Found")}' file
    192.168.1.1:22
    192.168.1.1:22
    192.168.1.1:22
    
    0 讨论(0)
  • 2021-01-24 04:15

    Input

    $ cat file
    date="2017-10-10" ip=192.168.1.1:22 inbound=100 outbound=100
    date="2017-10-10" ip=192.168.1.1:22 inbound=100
    date="2017-10-10" ip=192.168.1.1:22  outbound=100
    

    Output

    $ awk '{if(match($0,/inbound=[0-9]+/)){s=substr($0,RSTART,RLENGTH); print substr(s,index(s,"=")+1);next}print 0,"Not Found"}' file
    100
    100
    0 Not Found
    

    Explanation

    awk '{
    
        # Search for word inbound=[0-9]+ in record/line/row, if found then
        if(match($0,/inbound=[0-9]+/))
        {
            # Extract word
            s=substr($0,RSTART,RLENGTH)
    
            # Print value which is after "="  
            print substr(s,index(s,"=")+1)
    
            # Go to next line
            next
        }
    
          # If above condition is not true then word inbound not found in line/row/record
    
          print 0,"Not Found"
      }
     ' file
    
    0 讨论(0)
提交回复
热议问题