-1

I have this :

 S=`cat sales.txt`;
    echo "$S" | cut -d: -f5 | grep -E '[0-9][0-9][0-9]' | wc -l

My Textfile : name:id:number:type:value

Tom:8:987654:A:67           //1
Dick:9:7651:B:83            //2
Harry:5:21215121:A:92       //3
Molly:2:1231872231:A:44     //4
Megan:123:322:B:145         //5
Russia:8:99231272:B:530     //6
Boleh:123:29321831:C:5000   //7

I am trying to count the number of lines whereby the 5th column value are in the range of 0-999 .

When I try to run , it gives me count of 3 where it should be 6 .

1 Answers1

2

Your two digit numbers don't match the regex [0-9][0-9][0-9].

Also, storing a file in a variable and then printing it with echo is a really bad idea. See Why is printf better than echo?, but even more to the point, you can just do cat file in place of the echo command.

Actually, you can do the whole thing with Awk, and then you don't even need cat. Since you don't show any negative numbers, I'll just do a check for "less than one thousand."

awk -F: '$5 < 1000 {a++} END {print a}' sales.txt
Wildcard
  • 36,499