awk or perl one-liner to print line if second field is longer than 7 chars

前端 未结 5 1104
没有蜡笔的小新
没有蜡笔的小新 2021-01-13 21:34

I have a file of 1000 lines, each line has 2 words, separated by a space. How can I print each line only if the last word length is greater than 7 chars? Can I use awk RLE

相关标签:
5条回答
  • 2021-01-13 21:57
    perl -lane 'print if (length($F[$#F]) > 7)' fileName
    

    or

    perl -pae '$_ = "" if (length($F[$#F]) <= 7)' fileName
    
    0 讨论(0)
  • 2021-01-13 22:02
    perl -ane 'print if length($F[1]) > 7'
    
    0 讨论(0)
  • 2021-01-13 22:02
    perl -ane 'length $F[1] > 7 && print' <input_file>
    
    0 讨论(0)
  • 2021-01-13 22:06

    @OP, awk's RLENGTH is used when you call match() function. Instead, use the length() function to check for length of characters

    awk 'length($2)>7' file
    

    if you are using bash, a shell solution

    while read -r a b
    do
      if [ "${#b}" -gt 7 ];then
        echo $a $b
      fi
    done <"file"
    
    0 讨论(0)
  • 2021-01-13 22:07

    You can do:

    perl -ne '@a=split/\s+/; print if length($a[1]) > 7' input_file.txt
    

    Options used:

        -n  assume 'while () { ... }' loop around program
        -e  'command'    one line of program (several -e's allowed, omit programfile)
    

    You can use the auto-split option as used by Chris

        -a  autosplit mode with -n or -p (splits $_ into @F)
    
    0 讨论(0)
提交回复
热议问题