4

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) ??? }'

Casey
  • 41,449
  • 7
  • 95
  • 125
Marjer
  • 1,313
  • 6
  • 20
  • 31

3 Answers3

7

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

awk '$3!=5.5' filename
P.P
  • 117,907
  • 20
  • 175
  • 238
  • Thanks for resposne. `awk '$3!=5.5' filename` this command will work without a if condition, sorry if it's too basic. Right now i dont have a pc to test, give a try later. – Marjer Jun 19 '13 at 06:15
  • Yes. awk will print all lines by default. So the condition is to exclude the lines whose 3rd column has value `5.5`. – P.P Jun 19 '13 at 06:16
4

GNU safer sed

sed -r '/\s*(\S+\s+){2}5.5/d' file
captcha
  • 3,756
  • 12
  • 21
0
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
>
Vijay
  • 65,327
  • 90
  • 227
  • 319