Shreya
Shreya

Reputation: 649

Print line based on pattern

I have data in format

A  ((!(A1+A2)))
B  (A1A2)
C  (A1+A2)
D  (!(A1+A2)B1)
E  (!A1+!A2)
F  ((A1+A2)+A3+A4)
G  ((A1A2)+(A3A4))

I want output as

C  (A1+A2)
F  ((A1+A2)+A3+A4)
G  ((A1A2)+(A3A4))

I want to get that line which has only +

I used

awk -F' ' '$2 ~ /\+/' file

But this is listing all the lines which has ! along with + in them

Upvotes: 0

Views: 65

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133770

You should add 1 more condition to your code like following. You are already checking if 2nd field has + in it add an additional check if its NOT having ! too and then you should get your expected output.

awk '$2!~/!/ && $2~/\+/'  Input_file

Also you need not to use -F' ' option because by default itself awk uses space as a field separator for all lines, so I removed it from suggested code.

Upvotes: 2

anubhava
anubhava

Reputation: 786291

You may also try this awk that allows only + other than (, ), _ and alphanumerics:

awk '$2 ~ /^[()_[:alnum:]]+\+[()+_[:alnum:]]+$/' file

C  (A1+A2)
F  ((A1+A2)+A3+A4)
G  ((A1A2)+(A3A4))

Upvotes: 1

Related Questions