Awk or perl one-liner to print a line if the second field is longer than 7 characters

I have a file of 1000 lines, each line has 2 words, separated by a space. How to print each line only if the length of the last word is more than 7 characters? Can i use awk RLENGTH? is there any easy way in perl?

+4
source share
5 answers

@OP, awk RLENGTH is used when you call the match() function. Use the length() function instead to check the length of characters

 awk 'length($2)>7' file 

if you use bash, shell solution

 while read -rab do if [ "${#b}" -gt 7 ];then echo $a $b fi done <"file" 
+10
source
 perl -ane 'print if length($F[1]) > 7' 
+9
source

You can do:

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

Used options:

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

You can use the auto split option used by Chris

  -a autosplit mode with -n or -p (splits $ _ into @F)
+3
source
 perl -ane 'length $F[1] > 7 && print' <input_file> 
+2
source
 perl -lane 'print if (length($F[$#F]) > 7)' fileName 

or

 perl -pae '$_ = "" if (length($F[$#F]) <= 7)' fileName 
0
source

Source: https://habr.com/ru/post/1303658/


All Articles