Listing non zero values from file

Legends,

I have following contents in the file and i want to list out non-zero values only out of it.

OPge1 03
OPge10 121
OPge11 3
OPCge12 0
OPCge13 0
OPge14 25
OPC15 0

I am using following loop; but not getting desired results

for line in `cat /tmp/raw`
do
Name=`echo $line | awk -F' ' '{print $1}'`;
Count=`echo $line | awk -F' ' '{print $2}'`;
if [ "${Count}" != 0 ];
then
echo "$Name and $Count"
fi
done

Please help if

awk

or any other will be good way to do it

Hello sdosanjh,

With awk here is one of the solution.

awk '$2' filename
OR
awk '($2!=0) {print}' filename

Output will be as follows.

OPge1 03
OPge10 121
OPge11 3
OPge14 25

Thanks,
R. Singh

1 Like

@Ravinder :slight_smile:
thanks

If the text string in column 2 that you wish to exclude will always be described as a space followed by a single zero and then the end of line, that would give the regular expression 0$ that you could use in grep like this, which may be quicker:-

grep " 0$" filename
  • The leading space is important to make sure you don't exclude records that have values such as 30, 40, 50 etc.
  • The trailing $ marks the end of record so you don't exclude 101, 1024 etc.

If you have to evaluate the column in case there may be leading zeros too, then awk is the way unless it's a single leading zero, so you might need a single or double zero in which case:-

egrep -v " 0$| 00$" filename

I hope that this gives you an option to consider.

Robin

1 Like

@rbatte, that works too.