Grep multiple words with not null value

Hi,

I want to grep a file if any one (GH, IJ, KL) is not null. If it is null i dont want to pull anything.

cat file | awk '{print ($1)}'
Parameters are : AB=123;CD=456;EF=6789;
cat file | awk '{print ($2)}'
GH=456;IJ=789;KL=1011

eg:
Contents in file:

Parameters are : AB=123;CD=456;EF=6789;GH=;IJ=;KL=
Expected output: <blank>

Parameters are : AB=123;CD=456;EF=6789;GH=456;IJ=789;KL=1011
Expected Output: AB=123;CD=456;EF=6789;GH=456;IJ=789;KL=1011

Please help.

try

awk -F ";" '{for(i=1;i<=NF;i++){if($i ~ /GH|IJ|KL/ && length($i) <=3){$0="Blannk";break}}}1' OFS=";" file

also can be written as

egrep -v "GH=;|IJ=;|KL=$" file

pamu

1 Like

this works for all

awk -F\; '{for(i=1;i<=NF;i++){split($i,_,"=");if(!_[2])next;}print $0}' filename

---------- Post updated at 09:34 PM ---------- Previous update was at 09:32 PM ----------

$ cat a.txt
AB=123;CD=456;EF=6789;GH=;IJ=;KL=
AB=123;CD=456;EF=6789;GH=456;IJ=789;KL=1011

$ awk -F\; '{for(i=1;i<=NF;i++){split($i,_,"=");if(!_[2])next;}print $0}' a.txt
AB=123;CD=456;EF=6789;GH=456;IJ=789;KL=1011

Thanks a lot Pamu...

I tried the code and it was working fine...

---------- Post updated at 09:08 AM ---------- Previous update was at 09:07 AM ----------

Thanks itkamaraj for your reply.

---------- Post updated at 09:37 AM ---------- Previous update was at 09:08 AM ----------

In all these cases if any one parameter is null it will not pull values. I have to get values if any one parameter (GH, IJ, KL) is not null.

Please help.

Try

awk '/GH=[0-9]+;|IJ=[0-9]+;|KL=[0-9]+$/' file

which is equivalent to

grep -E 'GH=[0-9]+;|IJ=[0-9]+;|KL=[0-9]+$' file

Thanks RudiC for the quick reply.

I tried both the codes. It is pulling the value if IJ is having a value and GH and KL is null. In all other cases (GH with value and all other null/ KL with value and all other null) it is not pulling the value.

The file looks something like this
are or
MI=a12345;MR=b1234;CI=b123;CI=1234;CN=45;A=49;TR=21;EC=130;ED=We again.;BR=;BRC=;BRD=

are or
MI=a12345;MR=b1234;CI=b123;CI=1234;CN=45;A=49;TR=21;EC=130;ED=We again.;BR=1234;BRC=00;BRD=Good

I need to pull entire file if any parameters BR, BRC, or BRD have non null values.

Assuming you have three fields to check only..

awk -F ";" '{a=0;for(i=1;i<=NF;i++){if($i ~ /BR|BRC|BRD/){split($i,P,"=");if(P[2] == ""){a++}}}{if(a==3){$0="blank"}}}1' OFS=";" file
1 Like

Strange - both versions yield identical results on your post #1 sample file:

$ grep -E "GH=[0-9]+;|IJ=[0-9]+;|KL=[0-9]+$" file   - OR -
$ awk '/GH=[0-9]+;|IJ=[0-9]+;|KL=[0-9]+$/' file
Parameters are : GH=;IJ=789;KL=
Parameters are : GH=456;IJ=;KL=
Parameters are : GH=;IJ=789;KL=
Parameters are : GH=;IJ=;KL=23
Parameters are : GH=;IJ=;KL=23
Parameters are : GH=646;IJ=;KL=
Parameters are : GH=456;IJ=789;KL=2323

Just the line with all three fields empty is suppressed.

btw - PLEASE use code tags!

Thanks a lot Pamu... :slight_smile:

It is working as expected...

NOW I see - despite your first sample you are NOT using digits only in the fields.Try

grep -E 'BR=[[:alnum:]]+;|BRC=[[:alnum:]]+;|BRD=[[:alnum:]]+$' file
MI=a12345;MR=b1234;CI=b123;CI=1234;CN=45;A=49;TR=21;EC=130;ED=We again.;BR=1234;BRC=00;BRD=Good

and adapt other solutions if need be.

1 Like

Thanks RudiC...

Both codes worked fine...