Home > Software engineering >  Remove lines where the 10th column contains empty fields in text file in bash
Remove lines where the 10th column contains empty fields in text file in bash

Time:04-14

I have a huge tab-delimited text file with 10 columns. Now I want to delete all rows from the file that contain no value in column 10.

For example:

a b c d e f g h i j
4 6 8 9 4 2 1 6 4 2
1 5 9 8 5 1 8 3 6 
1 6 8 5 4 7 7 9 4 7
4 5 8 9 9 2 1 8 4 
3 4 7 5 8 8 2 5 3 6

Expected output:

a b c d e f g h i j
4 6 8 9 4 2 1 6 4 2
1 6 8 5 4 7 7 9 4 7
3 4 7 5 8 8 2 5 3 6

I would like to use something like:

awk '$10 == ""' print $0 file

CodePudding user response:

Your command was almost there. You can try this:

awk '$10 != "" {print}' file
  • $10 != "" This tests if the 10th field is not empty
  • print prints the entire line

CodePudding user response:

You can print each line where col 10 isn't empty using

awk '{if ($10) print}' file.txt

$ cat file.txt
a b c d e f g h i j
4 6 8 9 4 2 1 6 4 2
1 5 9 8 5 1 8 3 6
1 6 8 5 4 7 7 9 4 7
4 5 8 9 9 2 1 8 4
3 4 7 5 8 8 2 5 3 6
$
$
$ awk '{if ($10) print}' file.txt
a b c d e f g h i j
4 6 8 9 4 2 1 6 4 2
1 6 8 5 4 7 7 9 4 7
3 4 7 5 8 8 2 5 3 6
$

CodePudding user response:

I managed it using a "simple" grep:

grep $'.\t.\t.\t.\t.\t.\t.\t.\t.\t.' file.txt

The '.' stands for any character, the \t for a TAB character.

  •  Tags:  
  • bash
  • Related