awk partly string match (if column/word partly matches)

前端 未结 5 1723
情深已故
情深已故 2020-12-12 14:40

My dummy file looks like this:

C1    C2    C3    
1     a     snow   
2     b     snowman 
snow     c     sowman

I want to get line if ther

相关标签:
5条回答
  • 2020-12-12 15:09
    awk '$3 ~ /snow/ { print }' dummy_file 
    
    0 讨论(0)
  • 2020-12-12 15:15

    Print lines where the third field is either snow or snowman only:

    awk '$3~/^snow(man)?$/' file
    
    0 讨论(0)
  • 2020-12-12 15:24

    GNU sed

    sed '/\s*\(\S\+\s\+\)\{2\}\bsnow\(man\)\?\b/!d' file
    

    Input:

    C1    C2    C3    
    1     a     snow   
    2     b     snowman 
    snow     c     sowman
          snow     snow     snowmanx
    

    ..output:

    1     a     snow
    2     b     snowman
    
    0 讨论(0)
  • 2020-12-12 15:26

    Maybe this will help

    http://www.math.utah.edu/docs/info/gawk_5.html

    awk '$3 ~ /snow|snowman/' dummy_file
    
    0 讨论(0)
  • Also possible by looking for substring with index() function:

    awk '(index($3, "snow") != 0) {print}' dummy_file
    

    Shorter version:

    awk 'index($3, "snow")' dummy_file
    
    0 讨论(0)
提交回复
热议问题