Delete column with exact string in specific col

Hi,
my file structur looks like

File structure looks:
GeneID protein_gi Symbol
1246500 10954455 repA1
1246501 10954457 repA2
1246502 10954458 leuA

But some of the cases do not have record for protein id. for example:

1343044 - orf01

I want to remove those rows.

But I tried

awk -F, '$2==!/-/' file.txt >test1.txt 
awk -F' ', '$2==!/-/' file.txt >test1.txt 

None of them are working.
Can anybody please help? Thanks a lot,Mitra

---------- Post updated at 06:51 AM ---------- Previous update was at 06:48 AM ----------

I also tried

awk -F' ', '$2=='-'' file.txt >test1.txt  

But got error like

awk: syntax error at source line 1
 context is
	 >>>  <<< 
awk: bailing out at source line 1

try this

awk '{if ($2!="-") print}' file.txt

or

awk '$2!="-" {print} ' file.txt
1 Like

Is your file comma seperated? if space seperated..

 
awk '$2 ~ !/-/{print $0}' filename
1 Like
grep -v "\-" file
2 Likes

Thanks a lot all for your help. It worked perfectly with krishmath and vidyadhar85's suggestions...
Just Ice, thanks to you also, but as I have "-" in 3rd Gene symbol col also sometimes, thus I prefer to use column number.
Thanks a lot to all again. :smiley:

---------- Post updated at 07:38 AM ---------- Previous update was at 07:20 AM ----------

Hello all,
Can anybody suggest me why the new file sizes created by all above commands differ? only first two are identical with awk...

awk '{if ($2!="-") print}' file.txt

or

awk '$2!="-" {print} ' file.txt

But with

awk '$2 ~ !/-/{print $0}' filename

and

grep -v "\-" file

provide different file size... I am comparing as my input file is same, and for this occasion I have no '-' in other cols.

Any suggestion whould be good.
Thanks,
Mitra

I guess the below command stops printing after it encounters a "-" in 2nd column.

awk '$2 ~ !/-/{print $0}' filename

The

grep -v

should work fine. Are you sure you don't have any other "-" in the file?

1 Like

Thanks krishmaths,
Yes I guess that is the case. As first two commands deliver the file with size >300MB
But other just over 200MB.

No I am not sure..in some cases files may have - in other cols...thus I also prefer $2!="-"
Thanks for explaining.
Mitra.

You could do a

grep "-" file.txt

to find out if there are any "-" in your file.

---------- Post updated at 02:15 PM ---------- Previous update was at 02:03 PM ----------

This might need a slight change as below

awk '$2 !~ /-/ {print $0}' filename

thanks krishmaths,
yes I did that. But what I ment to say is, I don't have "-" in any other col for the first file. but in another I had. Thus I realized that must be the problem while other codes stopped.
Thus I preferred specifying col no.
Thanks,
Mitra

Even shorter:

awk '$2!="-"' file