Delete a line with a specific pattern in one field

Below is my input file, input.txt :

Value Value1 value2
5 1 2
1 4 3
2 1 5.5
0 0 0
4 1 0

I need to find the value (5.5) in the third column, if it is found, I need to completely delete the row.

And I need the output, as shown below, output.txt :

Value Value1 value2
5 1 2
1 4 3
0 0 0
4 1 0

I tried the awk command to delete a line, but I am stuck with the bottom (???). It does not follow how to remove the entire string from the awk command. please suggest a way to achieve this. Thank you
awk -F"\t" '{if($3==5.5) ??? }'

+4
source share
3 answers

If you want to exclude all rows with the third value of column 5.5 , then:

 awk '$3!=5.5' filename 
+7
source

GNU safer sed

 sed -r '/\s*(\S+\s+){2}5.5/d' file 
+4
source
 perl -lane 'next if($F[2]=~/5.5/);print' your_file 

if the third column has 5.5, then this row will be deleted. But if the third column is 5.52 or 15.53, then with the above command they will be deleted. Therefore, if you want to delete a line only if it has an exact exact match, use below:

 perl -lane 'next if($F[2]=~/\b5.5\b/);print' your_file 

checked below:

 > cat temp Value Value1 value2 5 1 2 1 4 3 2 1 5.51 2 1 5.5 0 0 0 4 1 0 > 

Using the first command:

 > perl -lane 'next if($F[2]=~/5.5/);print' temp Value Value1 value2 5 1 2 1 4 3 0 0 0 4 1 0 > 

With the second command:

 > perl -lane 'next if($F[2]=~/\b5.5\b/);print' temp Value Value1 value2 5 1 2 1 4 3 2 1 5.51 0 0 0 4 1 0 > 
0
source

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


All Articles