I want to grep the text value in UNIX shell file

Hi Guys,

I want to grep the text/code value in my shell script.

Sample Code:

-H "fjkldj skdfjk2342 kj s2323"

Here, the code value is in double quotes after -H. This value I have to pic and put it in variable to pass a parameter value.

Can any one help me on this!

Thanks in advance!

Regards,
Lakshman

Hello Lakshman,

Welcome to forums, please use code tags for commands/codes/inputs which you are using in your posts as per forum rules. You can go through the rules of forum in following link too.

For your requirement following may help you in same.

awk '{match($0,/\".*\"/);print substr($0,RSTART+1,RLENGTH-2)}'  Input_file

Output will be as follows, you can take this into a variable as per your need too.

fjkldj skdfjk2342 kj s2323

Thanks,
R. Singh

1 Like

Thanks for your reply, but there are many code values in my script, I want exactly after -H code value only.

Hello Lakshman,

From your post I am understanding you need to catch string -H "string" , if this is the case please use following, if not please give us sample inputs and exact required output for same, we can help you in same.

awk '{match($0,/\-H \".*\"/);print substr($0,RSTART+4,RLENGTH-5)}'  Input_file

Output will be as follows.

fjkldj skdfjk2342 kj s2323

Thanks,
R. Singh

1 Like

Try also

awk -F\" '$1~/-H/ {print $2}' file
fjkldj skdfjk2342 kj s2323

along with the below code a complete shell script is there. But I need to grep the value only after -H"asdfgsdfgsdfgg5345dg4545" with in double quotes.

/sfd/fgf/ghg/dsfds/bin/gdsfgsdfg "/fdsf/dfg/dfgh/gfhfgh/log/ghfghfgh/" -w "sdfgsdfgsdfg"
"-fgsdfgsdfg -r\"dfgsdfg.txt\" -H"asdfgsdfgsdfgg5345dg4545" -e5 -e12
-dfsgsdf -dfgdfg -dfgdfg -sgdfgsdfg -gfdfgfhfgh -dsfgdsfgsdfg -sdfgdfg

Can any one help me on this?

sed -n 's/.*-H *"\([^"]*\)".*/\1/p' file

Hello lakshmanraok,

Following may help you in same.

echo -H\"asdfgsdfgsdfgg5345dg4545\" | awk '{match($0,/H\".*\"/);print substr($0,RSTART,RLENGTH)}'
OR
awk '{match($0,/H\".*\"/);A=substr($0,RSTART,RLENGTH);if(A){print A}}'  Input_file

Output will be as follows.

H"asdfgsdfgsdfgg5345dg4545"

Thanks,
R. Singh

Can you please explain this command, how it works?

---------- Post updated at 06:42 AM ---------- Previous update was at 06:03 AM ----------

If I am giving this command

awk '{match($0,/-G\".*\"/);A=substr($0,RSTART,RLENGTH);if(A){print A}}' file name

I am getting value below while runing above command

-G"sdfsdg4353246dfgsdf5464" - fsdf -dsfsd -sdfsdf

Please suggest me on this.

I need only value that is with double quotes after -G

Hello lakshmanraok,

Following may help you in same.

awk '{match($0,/H\".*\"/);A=substr($0,RSTART+2,RLENGTH-3);if(A){print A}}'  Input_file

Here I am matching regex match($0,/H\".*\"/) and keywords RSTART, RKENGTH will be set according to matching of regex so I am taking it's values to a variable named A and if A is NOT empty then print it. Hope this helps, you can also read man awk for more information on same.

Thanks,
R. Singh

Hi Guys,

I am getting the below complete value,

a45ddfg_654dfg_fgf65_hghjhj42334" 
-t1 -t23 -tyutyu -hgjghj   -ertert -ghfgh -dfgdf -dfgdfg -ghjhj

while executing the below command:

awk '{match($0,/\-G\".*\"/);A=substr($0,RSTART+4,RLENGTH-2);if(A){print A}}' file name

the actual line in my shell script is:

/bnbn/er/hhhh12/gggg/bin/fg_fghfghfgh  
"/bnbn/hg/vbn12/xccvb/log/fgfgh_1/" 
-w "dfgd:dfgdfg:dfgdfg" 
" -ghjghjgh 
-E\"hgjghj.txt\"  
-G"a45ddfg_654dfg_fgf65_hghjhj42334" 
-t1 -t23 -tyutyu -hgjghj   -ertert -ghfgh -dfgdf -dfgdfg -ghjhj
"

but I want only the highlighted value.

thanks in advance!

Regards,
Lakshman

Hello Lakshman,

I am not sure you tried my solutions in privious posts or not, following is working for me.

awk '{match($0,/G\".*\"/);A=substr($0,RSTART+2,RLENGTH-3);if(A){print A}}'  Input_file

Output is as follows.

a45ddfg_654dfg_fgf65_hghjhj42334

Thanks,
R. Singh

No ...I am not getting using this command , because still i am getting some more text after the value:

a45ddfg_654dfg_fgf65_hghjhj42334 -dfgdsfg -0dfgdf -gdfg

like this, why because my code value with in another two double quotes like

" -G " sddfdfgfgdfgfg" "

below is the sample code

" -ghjghjgh 
-E\"hgjghj.txt\"  
-G"a45ddfg_654dfg_fgf65_hghjhj42334" 
-t1 -t23 -tyutyu -hgjghj   -ertert -ghfgh -dfgdf -dfgdfg -ghjhj
"

I dont know why its not showing ending with " at code value after G?

Can you suggest me on this?

Thanks in advance!

awk -F\" 'sub(/^.*-H *"/,"") && sub(/".*$/,"")' file
asdfgsdfgsdfgg5345dg4545

---------- Post updated at 14:54 ---------- Previous update was at 14:50 ----------

Exchange the H in above with G to get

a45ddfg_654dfg_fgf65_hghjhj42334

from your sample.