Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

awk

perl

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 RLENGTH? is there an easy way in perl?

like image 548
paul44 Avatar asked Nov 29 '22 11:11

paul44


2 Answers

perl -ane 'print if length($F[1]) > 7'
like image 20
Chris Jester-Young Avatar answered Dec 04 '22 04:12

Chris Jester-Young


@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"
like image 123
ghostdog74 Avatar answered Dec 04 '22 05:12

ghostdog74