Awk or Perl solution to delete rows with incomplete data

Is there an Awk or Perl single line that can delete all lines that do not have complete data. I often need 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 to delete a row that does not matter in field 2?

How to delete a line that does not matter in one of ANY field?

I tried to do something like this:

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

Thanks.

+4
source share
6 answers

In awk, if you know that each line must have exactly three elements:

 awk -F'\t+' 'NF == 3' INFILE > OUTFILE 
+4
source

For a specific solution:

 awk -F'\t' '$2 != ""' input.txt > output.txt 

For a general solution:

 awk -F'\t' -vCOLS=3 '{ valid=1; for (i=1; i<=COLS; ++i) { if ($i == "") { valid=0; break; } } if (valid) { print; } }' input.txt > output.txt 
+2
source

I would just look at consecutive tabs or at the leading tab or trailing:

 perl -ne 'next if /\t\t/ or /^\t/ or /\t$/; print' tabfile 
+2
source
 perl -lane 'print if $#F == 2' INFILE 
+1
source
 perl -F/\t/ -nle 'print if @F == 3' 1.txt > 2.txt 
0
source

Just use awk 'NF == 3' INFILE > OUTFILE and it will just use a space (tabs, spaces, etc.) as a field separator.

0
source

Source: https://habr.com/ru/post/1380761/


All Articles