Foramgoram
Foramgoram

Reputation: 13

How does this awk command works?

I have the below data in file and want to separate the lines into two variables - one having the version info and second one with no version info:

3f0e86dd5592                             1969-12-31T19:00:00
0cbf659a22db                             1969-12-31T19:00:00
9dd121133805  1.0.48                     1969-12-31T19:00:00
4daa44734d2e  1.0.45,1.0.47,1.0.42       1969-12-31T19:00:00

If I run below, I don't get the intended result (which I think, should give me the lines with no version info) .

awk '{ if ($2 == "") { print } }' file.txt 

But running below, gives me lines with no version:

awk '{ if ($3 == "") { print } }' file.txt 

Why is that?

Upvotes: 0

Views: 70

Answers (3)

rpoleski
rpoleski

Reputation: 998

These lines have different number of fields. First two lines have 2 fields and the next two have 3 fields each. Use NF==3 as a condition. $3=="" doesn't make sense.

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 246837

With fixed-width data, if you're using GNU awk, you can define with size of each field. Then empty fields will consist of only spaces:

gawk 'BEGIN {FIELDWIDTHS = "14 27 19"} $2 ~ /^[[:space:]]+$/' file
3f0e86dd5592                             1969-12-31T19:00:00
0cbf659a22db                             1969-12-31T19:00:00

Upvotes: 3

Mahmoud Odeh
Mahmoud Odeh

Reputation: 950

if I understand your question you make it sample with NF == 2, If you notice any mistake, please don't hesitate to correct me

awk 'NF==2{print}' file

or:

awk '{if(NF==2) print}' file

input :

3f0e86dd5592                             1969-12-31T19:00:00
0cbf659a22db                             1969-12-31T19:00:00
9dd121133805  1.0.48                     1969-12-31T19:00:00
4daa44734d2e  1.0.45,1.0.47,1.0.42       1969-12-31T19:00:00

output :

3f0e86dd5592                             1969-12-31T19:00:00
0cbf659a22db                             1969-12-31T19:00:00

Upvotes: 0

Related Questions