maqritos
maqritos

Reputation: 55

read line by line awk and if

I have a File called contenido.txt the file have inside the next table

Nombre column1 column2 valor3
Marcos    1      0     0
Jose      1      0     0
Andres    0      0     0
Oscar     1      0     0
Pablo     0      0     0

I need a final file or a print of the lines that only has 0 in the column2

could you help me please?

cat contenido.txt | while read LINE; do
var=$(cat $LINE | awk '{print $2}')

if ["$var" == 0]
then
echo $LINE | awk '{print $1}'
fi
done

Upvotes: 1

Views: 321

Answers (2)

Kent
Kent

Reputation: 195039

After reading your codes, the column 2 you meant is actually the 2nd column( the column with header "column1"), it is not the column with header "column2". So this line will help you:

awk 'NR==1{print;next}$2==0' file

test with your data

kent$  echo "Nombre column1 column2 valor3
Marcos    1      0     0
Jose      1      0     0
Andres    0      0     0
Oscar     1      0     0
Pablo     0      0     0"|awk 'NR==1{print;next}$2==0'
Nombre column1 column2 valor3
Andres    0      0     0
Pablo     0      0     0

and the 2nd part of your codes seem that extracting the first column (names?) out. You can do this in one shot with awk (ignore the header):

kent$  echo "Nombre column1 column2 valor3
Marcos    1      0     0
Jose      1      0     0
Andres    0      0     0
Oscar     1      0     0
Pablo     0      0     0"|awk '$2==0{print $1}'      
Andres
Pablo

Upvotes: 2

user1907906
user1907906

Reputation:

column2 is $3 in awk. So:

$ awk '$3 == 0' < in.txt
Marcos    1      0     0
Jose      1      0     0
Andres    0      0     0
Oscar     1      0     0
Pablo     0      0     0

{print $0} is the implicit action.

Upvotes: 1

Related Questions