awk to remove line if field has symbols in it

Trying to use awk to remove a line only if $1 contains either ; or : . Thje awk below runs but no lines are removed. Thank you :).

awk

awk '$1 !~ /;/ || $1 !~ /:/ { print }' file

file

AARS2;TMEM151B 1
AASS 2
ABAT 3
ABCA1 3
ABCA10 1
ABCA12 2 
ABCA13 1
ABCA13:AX746840 2
ABCA2 5

desired output

AASS 2
ABAT 3
ABCA1 3
ABCA10 1
ABCA12 2 
ABCA13 1
ABCA2 5
awk '$1 !~ /;|:/' file
awk '$1 !~ /[;:]/' file
1 Like

Hi cmccabe,
The code you have above will print any line where $1 does not contain a semicolon OR does not contain a colon which will print any line where $1 does not contain both a semicolon and a colon. Either of the suggestions Aia provided should do what you want. Given the way you stated your problem, I would usually use Aia's second suggestion.

It could also be written as:

awk '$1 ~ /;/ || $1 ~ /:/ { next } 1' file
1 Like

Another approach is to only print a line if it does not contain ; AND it does not contain :

awk '$1 !~ /;/ && $1 !~ /:/ { print }' file
1 Like

Thank you all :slight_smile:

Hello cmccabe,

Not sure your complete moto of this script but in case you don't want to have those lines which have either ; or | not just only first field then you could use following command too for same.

grep -v '[;:]'  Input_file

Output will be as follows.

AASS 2
ABAT 3
ABCA1 3
ABCA10 1
ABCA12 2 
ABCA13 1
ABCA2 5

Thanks,
R. Singh