Marjer
Marjer

Reputation: 1403

Remove 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 search the value(5.5) in the 3rd column, if found i need to remove the row entirely.

And i need the output like below, output.txt:

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

i have tried awk command to remove the row, but I'm stuck with the below part(???). Don't to how to remove the row entire row from awk command. please suggest way to achieve this. Thanks!
awk -F"\t" '{if($3==5.5) ??? }'

Upvotes: 4

Views: 3555

Answers (3)

captcha
captcha

Reputation: 3756

GNU safer sed

sed -r '/\s*(\S+\s+){2}5.5/d' file

Upvotes: 4

Vijay
Vijay

Reputation: 67221

perl -lane 'next if($F[2]=~/5.5/);print' your_file

if the third column has 5.5 then that row will be removed. But if the third column is 5.52 or 15.53, then with the above command they will be removed. So if you want to only remove the row if it has 5.5 exact match then use below:

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

tested 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
> 

With 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
>

Upvotes: 0

P.P
P.P

Reputation: 121377

If you want exclude all lines with 3rd column's value 5.5 then:

awk '$3!=5.5' filename

Upvotes: 7

Related Questions