I have a file of 1000 lines, each line has 2 words, seperated 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?
views:
217answers:
4+1: good use of auto split.
codaddict
2010-03-10 11:53:18
@Sinan: Can't believe you turned it into a code golf. :-P
Chris Jester-Young
2010-03-10 12:12:23
+2
A:
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)
codaddict
2010-03-10 11:51:43
+6
A:
@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"
ghostdog74
2010-03-10 12:03:23
+1 awk rocks. Note that it automatically prints all matching lines so you dont have to mess with any printf calls!
Martin Wickman
2010-03-10 12:15:47