0

I have a file whose content is

ae-scc-san-vserver01 /vol/ae_drac02_1_oradminvg_vol/ae_drac02_1_oradminvg_lun               hdisk0          fcs0       FCP        150g    C
ae-scc-san-vserver01 /vol/ae_drac02_1_rootvg_vol/ae_drac02_1_rootvg_lun                     hdisk1          fcs0       FCP        100g    C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskcrs03SVC_vol/ae_drac02_1_2_hdiskcrs03SVC_lun   hdisk11         fcs0       FCP        16.0g   C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskdata01SVC_vol/ae_drac02_1_2_hdiskdata01SVC_lun hdisk12         fcs0       FCP        240.0g  C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskdata02SVC_vol/ae_drac02_1_2_hdiskdata02SVC_lun hdisk13         fcs0       FCP        240.0g  C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskdata03SVC_vol/ae_drac02_1_2_hdiskdata03SVC_lun hdisk14         fcs0       FCP        240.0g  C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskdata04SVC_vol/ae_drac02_1_2_hdiskdata04SVC_lun hdisk15         fcs0       FCP        240.0g  C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskdata05SVC_vol/ae_drac02_1_2_hdiskdata05SVC_lun hdisk17         fcs0       FCP        240.0g  C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskdata06SVC_vol/ae_drac02_1_2_hdiskdata06SVC_lun hdisk18         fcs0       FCP        240.0g  C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskdata12SVC_vol/ae_drac02_1_2_hdiskdata12SVC_lun hdisk19         fcs0       FCP        250g    C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskdata13SVC_vol/ae_drac02_1_2_hdiskdata13SVC_lun hdisk20         fcs0       FCP        250g    C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskdata14SVC_vol/ae_drac02_1_2_hdiskdata14SVC_lun hdisk21         fcs0       FCP        250g    C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskdata15SVC_vol/ae_drac02_1_2_hdiskdata15SVC_lun hdisk22         fcs0       FCP        250g    C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskdata16SVC_vol/ae_drac02_1_2_hdiskdata16SVC_lun hdisk23         fcs0       FCP        250g    C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskdata17SVC_vol/ae_drac02_1_2_hdiskdata17SVC_lun hdisk24         fcs0       FCP        250g    C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskdata18SVC_vol/ae_drac02_1_2_hdiskdata18SVC_lun hdisk25         fcs0       FCP        250g    C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskdata19SVC_vol/ae_drac02_1_2_hdiskdata19SVC_lun hdisk26         fcs0       FCP        250g    C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskdata20SVC_vol/ae_drac02_1_2_hdiskdata20SVC_lun hdisk27         fcs0       FCP        250g    C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskdata21SVC_vol/ae_drac02_1_2_hdiskdata21SVC_lun hdisk28         fcs0       FCP        250g    C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskdata22SVC_vol/ae_drac02_1_2_hdiskdata22SVC_lun hdisk29         fcs0       FCP        250g    C
ae-scc-san-vserver01 /vol/ae_drac02_1_swapvg_vol/ae_drac02_1_swapvg_lun                     hdisk3          fcs0       FCP        32.0g   C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskdata23SVC_vol/ae_drac02_1_2_hdiskdata23SVC_lun hdisk30         fcs0       FCP        250g    C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskfra01SVC_vol/ae_drac02_1_2_hdiskfra01SVC_lun   hdisk31         fcs0       FCP        240.0g  C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskfra02SVC_vol/ae_drac02_1_2_hdiskfra02SVC_lun   hdisk32         fcs0       FCP        240.0g  C
ae-scc-san-vserver01 /vol/ae_drac02_1_u01vg_vol/ae_drac02_1_u01vg_lun                       hdisk4          fcs0       FCP        170.0g  C
ae-scc-san-vserver01 /vol/ae_drac02_1_exportsvg_vol/ae_drac02_1_exportsvg_lun               hdisk5          fcs0       FCP        100g    C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskcrs01SVC_vol/ae_drac02_1_2_hdiskcrs01SVC_lun   hdisk6          fcs0       FCP        16.0g   C
ae-scc-san-vserver01 /vol/ae_drac02_1_2_hdiskcrs02SVC_vol/ae_drac02_1_2_hdiskcrs02SVC_lun   hdisk7          fcs0       FCP        16.0g   C

I need to capture the server name. It starts with hdisk and ends with SVC. I tried the following:

sed 's/^*hdisk(.*)SVC.*/\1/' filename

I am getting this error:

sed: 1: "s/^*hdisk(.*)SVC.*/\1/": \1 not defined in the RE

Can someone please help me on how to capture it.

3 Answers 3

1

The problem is that hdisk...SVC occurs twice in the line, and .* is greedy.

If you have GNU grep, you can use the -P option to get non-greedy matching:

grep -oP '(?<=hdisk).+?(?=SVC)' file | sort -u

outputs

crs01
crs02
crs03
data01
data02
data03
data04
data05
data06
data12
data13
data14
data15
data16
data17
data18
data19
data20
data21
data22
data23
fra01
fra02
0

You need to escape the parens (and you also need to use an asterisk or some other method to match more than one character between hdisk and SVC). e.g.:

sed 's/^hdisk\([^\/]*\)SVC.*/\1/' filename

3
  • This gives me the entire row, i just need to get hdiskcrs03SVC from the entire row....as my output .....I'm not interested in other values Commented Jan 21, 2020 at 16:23
  • 1
    @NileshChaturvedi sed -n 's/.*hdisk\(.*\)SVC.*/\1/p' filename
    – user313992
    Commented Jan 21, 2020 at 16:27
  • @NileshChaturvedi I've changed the regex to only match non-slash characters. That should work for you. You could possibly do better depending on your exact specs (like only matching alphanumeric or whatever). Or switch to minimal matching. Commented Jan 21, 2020 at 16:36
0

You can also use an awk-based approach:

With GNU Awk:

awk 'match($2,/^\/vol[[:print:]]*hdisk([^_]+)SVC/,v) {print(v[1])}' filename

will match any line where the second column contains the pattern /vol<...>hdisk<...>SVC<...> and prints the string between hdisk and the first SVC, as underscores _ have been excluded.

Another approach (should be portable)

awk 'split($2,v,/hdisk|SVC/)>1 {print v[2];next}' filename

will use hdisk and SVC as field delimiters to split the second column into fields, and print the second field if more than one was found.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .