Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awk or Perl solution to delete lines with incomplete data

Tags:

awk

perl

Is there an Awk or Perl one-liner that can remove all rows that do not have complete data. I find myself frequently needing something like this. For example, I currently have a tab-delimited file that looks like this:

1    asdf    2
9    asdx    4
3    ddaf    6
5            4
2    awer    4

How can a line that does not have a value in field 2 be removed?

How could a line that does not have a value in one of ANY field be removed?

I was trying to do something like this:

awk -F"\t" '{if ($2 != ''){print $0}}' 1.txt > 2.txt

Thanks.

like image 233
drbunsen Avatar asked Feb 22 '23 10:02

drbunsen


1 Answers

In awk, if you know that every row should have exactly 3 elements:

awk -F'\t+' 'NF == 3' INFILE > OUTFILE
like image 82
schot Avatar answered Mar 03 '23 04:03

schot