Operating System - Linux
1830895 Members
1472 Online
110017 Solutions
New Discussion

awk question: Change /dev/dsk/c101t2d4 -> c101t2d4

 
SOLVED
Go to solution
Stuart Abramson
Trusted Contributor

awk question: Change /dev/dsk/c101t2d4 -> c101t2d4

Consider this: I use the following awk script to reduce the 'syminq -v -clar' report to one
line:

Device Physical Name : /dev/rdsk/c101t5d3
Device Serial ID : Ch2 CONT
Vendor ID : DGC
Product ID : CX700WDR5
Product Revision : HP03
Capacity (in KB) : 8838720
Clariion ID : APM00052802175
Device Clariion Name : 0117
Device State : ASSIGNED
VxVM DMP Device Type : N/A

Device Physical Name : /dev/rdsk/c101t5d4
Device Serial ID : Ch2 CONT
Vendor ID : DGC
Product ID : CX700WDR5
Product Revision : HP03
Capacity (in KB) : 70709760
Clariion ID : APM00052802175
Device Clariion Name : 0116
Device State : ASSIGNED
VxVM DMP Device Type : N/A

b. Do this:

awk '
/Device Phys/ {CTD=$5; next}
/Product ID/ {PROD=$4; next}
/Capacity/ {CAP=$5; next}
/Clariion ID/ {ARRAY=$4; next}
/Device Clar/ {LUN=$5; next}
/VxVM/ {print CTD, PROD, CAP/1000000, ARRAY, LUN}' \
syminq-v-clar

/dev/rdsk/c101t5d3 CX700WDR5 8.83872 APM00052802175 0117
/dev/rdsk/c101t5d4 CX700WDR5 70.7098 APM00052802175 0116

c. But I don't want "/dev/rdsk/c101t5d3".

I want "c101t5d3".

d. So, how do I modify the above little awk program to strip out the "/dev/dsk/".

I have been messing around with "sub" and "gsub", with no good result.
12 REPLIES 12
Ranjith_5
Honored Contributor

Re: awk question: Change /dev/dsk/c101t2d4 -> c101t2d4

Hi Stuert,

I assume your final out put is now

dev/rdsk/c101t5d3 CX700WDR5 8.83872 APM00052802175 0117
/dev/rdsk/c101t5d4 CX700WDR5 70.7098 APM00052802175 0116

Now to eliminate the /dev/rdsk you can input this output to

awk '{print $1 }'|cut -f 4 -d "/"

Regards,
Syam
Pat Lieberg
Valued Contributor

Re: awk question: Change /dev/dsk/c101t2d4 -> c101t2d4

I'm no awk expert, but you can change the field seperator or delimiter with -F.

My limited brain says to just re-awk the first element in each line with something like:

awk -F "/" {'print $4'}

I'm sure someone with more awk knowledge can find a better way to streamline it into your existing code.
A. Clay Stephenson
Acclaimed Contributor

Re: awk question: Change /dev/dsk/c101t2d4 -> c101t2d4

It's dumb to use the external command cut when awk can do this internally and more efficiently. Look at the awk function sub (or gsub)

sub(r,s,t)

Substitute s for the first match of the RE r in the string t. If argument t is not supplied, $0 is used. You can substitute a null string to remove matching RE's.

RE => regular expression

Sub returns 1 if okay and 0 otherwise.
If it ain't broke, I can fix that.
Mel Burslan
Honored Contributor

Re: awk question: Change /dev/dsk/c101t2d4 -> c101t2d4

how about piping your output string into this

... | sed -e "1,1s+/dev/dsk/++

I think it will eliminate the string.
________________________________
UNIX because I majored in cryptology...
curt larson_1
Honored Contributor

Re: awk question: Change /dev/dsk/c101t2d4 -> c101t2d4

using split would seem to work better

change

/Device Phys/ {CTD=$5; next}

to

/Device Phys/ {split($5,a,"/"); CTD=a[3];next;}
Sandman!
Honored Contributor

Re: awk question: Change /dev/dsk/c101t2d4 -> c101t2d4

Change the awk line that handles the "Device Phy" pattern slightly. The rest of the awk script remains unchanged:

=============================================
awk '
/Device Phys/ {CTD=z[split($5,z,"/")]; next}
/Product ID/ {PROD=$4; next}
/Capacity/ {CAP=$5; next}
/Clariion ID/ {ARRAY=$4; next}
/Device Clar/ {LUN=$5; next}
/VxVM/ {print CTD, PROD, CAP/1000000, ARRAY, LUN}' \
syminq-v-clar
=============================================

regards!
curt larson_1
Honored Contributor

Re: awk question: Change /dev/dsk/c101t2d4 -> c101t2d4

oops that probably should be a[4] instead of a[3], but you could figure that out.

sandman, nice syntax to get the last field. i'll have to write that one done.
RAC_1
Honored Contributor
Solution

Re: awk question: Change /dev/dsk/c101t2d4 -> c101t2d4

You can do
'{print substr(CTD,y,z)}'
There is no substitute to HARDWORK
Jean-Luc Oudart
Honored Contributor

Re: awk question: Change /dev/dsk/c101t2d4 -> c101t2d4

Hi,

you can pipe the output into :
sed -e "s/\/dev\/rdsk\///"

Regards
Jean-Luc
fiat lux
Leif Halvarsson_2
Honored Contributor

Re: awk question: Change /dev/dsk/c101t2d4 -> c101t2d4

Hi,

There is always several solutions, for this specific problem I would use the gsub function in awk as recommended by Clay. But, another method that I have often used with awk scripts is to pre-process the file before piping it to awk. In your case, you can translate the "/" to " " (using the tr command) and then "print $NF" in awk.
Bill Hassell
Honored Contributor

Re: awk question: Change /dev/dsk/c101t2d4 -> c101t2d4

Rather than use awk or cut to eliminate the leading directories, you can use the command designed to do this:

DEVFILE=/dev/rdsk/c101t5d3
DISK=$(basename $DEVFILE)

Or better yet, let the shell do the stripping:

DISK=${DEVFILE##*/}


Bill Hassell, sysadmin
Stuart Abramson
Trusted Contributor

Re: awk question: Change /dev/dsk/c101t2d4 -> c101t2d4

I used "substr", but "split" would also do it. Thanks all.