Operating System - HP-UX
1753707 Members
5121 Online
108799 Solutions
New Discussion юеВ

Re: first word of second field needed within an awk print command

 
SOLVED
Go to solution
Scott Lindstrom_2
Regular Advisor

first word of second field needed within an awk print command

I currently parse the output of an EMC command with the following awk command:
awk '{FS=":";print $1, $2, $3, $6, $8, $9}' > /tmp/inq_et_emc

But the command now outputs slightly different output than it used to. Here is some sample output of the EMC command:

/dev/rdsk/c2t2d0 :HP 73.4G:ST373207LC :HPC3 :3KT5H8PQ :71687369
/dev/rdsk/c2t4d0 :HP 73.4G:ST373207LC :HPC3 :3KT67CDH :71687369
/dev/rdsk/c2t6d0 :HP 73.4G:ST373307LC :HPC8 :3HZ7N6DM :71687369
/dev/rdsk/c5t0d0 :HP :DG072A3515 :HPD1 :BSA1P7A0 :71687369
/dev/rdsk/c5t0d0s1 :HP :DG072A3515 :HPD1 :BSA1P7A0 :512000
/dev/rdsk/c5t0d0s2 :HP :DG072A3515 :HPD1 :BSA1P7A0 :70765569
/dev/rdsk/c5t0d0s3 :HP :DG072A3515 :HPD1 :BSA1P7A0 :409600
/dev/rdsk/c10t0d0 :EMC :SYMMETRIX :5771 :279C8000 :14284801
/dev/rdsk/c10t0d1 :EMC :SYMMETRIX :5771 :279C9000 :14284801
/dev/rdsk/c10t0d2 :EMC :SYMMETRIX :5771 :279CA000 :14284801
/dev/rdsk/c10t0d3 :EMC :SYMMETRIX :5771 :279CB000 :14284801
/dev/rdsk/c10t0d4 :EMC :SYMMETRIX :5771 :279CC000 :14284801

So where I print field $2 to my output file (/tmp/inq_et_emc) I now get "HP 73.4G" whereas I used to only get "HP". This causes me trouble downstream when I read in the output file. (And I would like to avoid adding a field seperator to the new output file).

So what I really want is to only get the first word of field $2, in this case "HP". Is there a variation of substr that would work? I don't want to hardcode a start and end position. Or is there some other sub-command of awk I should be using?

TIA,
Scott

6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: first word of second field needed within an awk print command

Hi Scott:

# awk '{FS=":";split($2,a," ");print $1, a[1], $3, $6, $8, $9}'

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: first word of second field needed within an awk print command

What does the original output look like? I don't see 9 colon fields??

Perhaps you need to split on both space and colon?
awk -F'[ :]' '...'
Ivan Krastev
Honored Contributor

Re: first word of second field needed within an awk print command

Hi Scott,

Denis was right - if you don't specify the separator, the default one (space) was used.

In your case:
/dev/rdsk/c2t2d0 :HP 73.4G ...

1st is /dev/rdsk/c2t2d0
2nd is :HP
3rd is 73.4G

You should specify another separator (-F ":") or better post full output to take a look.

regards,
ivan
Scott Lindstrom_2
Regular Advisor

Re: first word of second field needed within an awk print command

Sorry - I posted the wrong awk command but the problem is still the same. Here is the awk command followed by the raw EMC command output. I still need the first word of the second variable.

inq -nodots -system | grep rdsk | grep -v -e ROM -e DVD | sort -k 1 | awk '{FS=":";print $1, $2, $3, $5, $6}' > /tmp/inq

Here is the raw output of the EMC command:

/dev/rdsk/c10t11d1 :EMC :SYMMETRIX :5771 :27A21000 :14284801
/dev/rdsk/c10t11d2 :EMC :SYMMETRIX :5771 :27A22000 :14284801
/dev/rdsk/c10t11d3 :EMC :SYMMETRIX :5771 :27A23000 :14284801
/dev/rdsk/c2t4d0 :HP 73.4G:ST373207LC :HPC3 :3KT67CDH :71687369
/dev/rdsk/c2t6d0 :HP 73.4G:ST373307LC :HPC8 :3HZ7N6DM :71687369
/dev/rdsk/c5t0d0 :HP :DG072A3515 :HPD1 :BSA1P7A0 :71687369
/dev/rdsk/c5t0d0s1 :HP :DG072A3515 :HPD1 :BSA1P7A0 :512000
/dev/rdsk/c5t0d0s2 :HP :DG072A3515 :HPD1 :BSA1P7A0 :70765569

When I do the print $2 on this field ":HP 73.4G:" I need only the "HP" output. Otherwise my next script that reads in this output gets confused.

I still need to try the suggestion I've rec'd so far; that may be the answer.
Scott Lindstrom_2
Regular Advisor

Re: first word of second field needed within an awk print command

James answer looks like it does it. Here is the raw output and the awk output he gave:

inq -nodots -system | grep rdsk | grep -v -e ROM -e DVD | sort -k 1|grep HP

/dev/rdsk/c2t0d0 :HP 73.4G:ST373307LC :HPC8 :3HZ7L6C1 :71687369
/dev/rdsk/c2t0d0s1 :HP 73.4G:ST373307LC :HPC8 :3HZ7L6C1 :512000
/dev/rdsk/c2t0d0s2 :HP 73.4G:ST373307LC :HPC8 :3HZ7L6C1 :70765737
/dev/rdsk/c2t0d0s3 :HP 73.4G:ST373307LC :HPC8 :3HZ7L6C1 :409600
/dev/rdsk/c2t2d0 :HP 73.4G:ST373207LC :HPC3 :3KT5H8PQ :71687369
/dev/rdsk/c2t4d0 :HP 73.4G:ST373207LC :HPC3 :3KT67CDH :71687369
/dev/rdsk/c2t6d0 :HP 73.4G:ST373307LC :HPC8 :3HZ7N6DM :71687369
/dev/rdsk/c5t0d0 :HP :DG072A3515 :HPD1 :BSA1P7A0 :71687369
/dev/rdsk/c5t0d0s1 :HP :DG072A3515 :HPD1 :BSA1P7A0 :512000
/dev/rdsk/c5t0d0s2 :HP :DG072A3515 :HPD1 :BSA1P7A0 :70765569
/dev/rdsk/c5t0d0s3 :HP :DG072A3515 :HPD1 :BSA1P7A0 :409600
/dev/rdsk/c5t1d0 :HP :DG072A3515 :HPD1 :BSA1P7A0 :71687369
/dev/rdsk/c5t2d0 :HP :DG072A3515 :HPD1 :BSA1P7A0 :71687369
/dev/rdsk/c5t3d0 :HP :DG072A3515 :HPD1 :BSA1P7A0 :71687369

inq -nodots -system | grep rdsk | grep -v -e ROM -e DVD | sort -k 1|grep HP| awk '{FS=":";split($2,a," ");print $1, a[1], $3, $5, $6}'

/dev/rdsk/c2t0d0 HP ST373307LC 3HZ7L6C1 71687369
/dev/rdsk/c2t0d0s1 HP ST373307LC 3HZ7L6C1 512000
/dev/rdsk/c2t0d0s2 HP ST373307LC 3HZ7L6C1 70765737
/dev/rdsk/c2t0d0s3 HP ST373307LC 3HZ7L6C1 409600
/dev/rdsk/c2t2d0 HP ST373207LC 3KT5H8PQ 71687369
/dev/rdsk/c2t4d0 HP ST373207LC 3KT67CDH 71687369
/dev/rdsk/c2t6d0 HP ST373307LC 3HZ7N6DM 71687369
/dev/rdsk/c5t0d0 HP DG072A3515 BSA1P7A0 71687369
/dev/rdsk/c5t0d0s1 HP DG072A3515 BSA1P7A0 512000
/dev/rdsk/c5t0d0s2 HP DG072A3515 BSA1P7A0 70765569
/dev/rdsk/c5t0d0s3 HP DG072A3515 BSA1P7A0 409600
/dev/rdsk/c5t1d0 HP DG072A3515 BSA1P7A0 71687369
/dev/rdsk/c5t2d0 HP DG072A3515 BSA1P7A0 71687369
/dev/rdsk/c5t3d0 HP DG072A3515 BSA1P7A0 71687369
James R. Ferguson
Acclaimed Contributor

Re: first word of second field needed within an awk print command

Hi (agaion) Scott:

My answer is the same as originally: you can use 'split' with a different field separator to achieve your goal:

# ... | awk '{FS=":";split($2,a," ");print $1,a[1],$3,$5,$6}' > tmp/inq_et_emc

Regards!

...JRF...