Search for Pattern as output between the matched String

Hello,

I have a file which has the below contents :

VG_name           LV_name             LV_size in MB    LV_option           LV_mountpoint                owner   group y
testdg                     rahul2lv        10                  "-A y -L"                  /home/abc2                         oracle    dba

I want to print only the pattern between double quotes. In the above case output should be following : -A y -L

Please help me to achieve this.

Thanks
Rahul

Hello rahul2662,

Following may help you in same, also you haven't mentioned if there are 1 or more occurrences of "" apart from shown in your sample post in a single line, so assuming you have only 1 occurrence of "" in a single line.

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

Output will be as follows.

A y -L

Thanks,
R. Singh

1 Like

Hello Ravinder ,

Yes there is only one occurence of "".

Also is there any simpler sed command to achieve this ?

Could you please explain following (RSTART and RLENGTH part) : A=substr($0,RSTART+2,RLENGTH-3)

Also thanks a lot for your help and this worked.

Thanks
Rahul

Hello rahul2662,

Could you please try following sed solution and let me know if this helps you.

sed -n 's/\(.*"\)\([^"].*\)\(".*\)/\2/p'  Input_file

Output will be as follows.

-A y -L

EDIT: Sorry was busy in a deployment so couldn't give explanation for code posted in POST#2. Following is the explanation for code posted in POST#2 as follows.

awk '{match($0,/".*"/);          ###### using match keyword in-built awk functionality for matching the regex into Input_file. Here patteren is match(LINE,REGEX).
A=substr($0,RSTART+2,RLENGTH-3); ###### creating a variable A whose value is equal to substring of line's starting point which is RSTART+2 and line's end point which is RLENGTH-3. NOTE: variables RSTART and RLENGTH are again awk's inbuilkt variables which will get values once a regex match is TRUE while using match keyword.
if(A){print A}}                  ###### Making sure here value of A is present then printing the value of variable A then.
' Input_file                     ###### mentioning the Input_file here for same.
 

Thanks,
R. Singh

1 Like

Try also

awk -F\" '{for (i=2; i<=NF; i+=2) print $i}' file
-A y -L
1 Like

If there is never more than one quoted string on a line, the awk can be simplified to:

awk -F\" 'NF>2{print $2}' file

and the sed could be simplified to:

sed -n 's/.*"\(.*\)".*/\1/p'  file
1 Like
awk '/\"/{for(i=2;i<=NF;i+=2){print $i}}' FS=\"  file
1 Like