Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filtering data 1

Status
Not open for further replies.

Kipnep70

Technical User
Nov 18, 2003
81
US
My input looks like the following:

Code:
Device Name  : "/dev/rmt/c56t0d0BESTnb"
Passthru Name: "/dev/sctl/c56t0l0"
Volume Header: ""
Port: -1; Bus: -1; Target: -1; LUN: -1
Inquiry    : "HP      Ultrium 3-SCSI  M66S"
Vendor ID  : "HP      "
Product ID : "Ultrium 3-SCSI  "
Product Rev: "M66S"
Serial Number: "MXP0903BDE"
WWN          : ""
WWN Id Type  : 0
Device Identifier: ""
Device Type    : SDT_TAPE
NetBackup Drive Type: 16
Removable      : Yes
Device Supports: SCSI-3
Flags : 0x0
Reason: 0x0
------------------------------------------------------------
Device Name  : "/dev/rmt/c57t0d0BESTnb"
Passthru Name: "/dev/sctl/c57t0l0"
Volume Header: ""
Port: -1; Bus: -1; Target: -1; LUN: -1
Inquiry    : "HP      Ultrium 3-SCSI  M66S"
Vendor ID  : "HP      "
Product ID : "Ultrium 3-SCSI  "
Product Rev: "M66S"
Serial Number: "HU10904G41"
WWN          : ""
WWN Id Type  : 0
Device Identifier: ""
Device Type    : SDT_TAPE
NetBackup Drive Type: 16
Removable      : Yes
Device Supports: SCSI-3
Flags : 0x0
Reason: 0x0

I want the output to be

/dev/rmt/c56t0d0BESTnb MXP0903BDE
/dev/rmt/c57t0d0BESTnb HU10904G41

Here is what I'm doing:

Code:
awk '/Device Name/ || /Serial Number/ {print $NF}' | sed 's/"//g' | awk '/dev/ {print d;d=""};{d=d" "$0}END{print d}'

Can you suggest a way to shorten it?
 
Hi

That is quit UUOS, as [tt]awk[/tt] can do the substitution itself.
Code:
awk '/Device Name/||/Serial Number/{gsub(/"/,"",$NF);printf"%s%s",$NF,/Serial Number/?RS:FS}'

[gray]# or[/gray]

awk -F '[: "]+' '/Device Name/||/Serial Number/{printf"%s%s",$(NF-1),/Serial Number/?RS:OFS}'
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].
Code:
 sed -n '/Device Name/h;/Serial Number/{H;g;s/\(^\|\n\)[^"]*"//g;s/"/ /;s/"//;p}'
Tested with GNU [tt]sed[/tt].

Feherke.
 
Wow. Nice. Can you explain what the awk statements are doing?

I understand it up until this point:

Code:
...,/Serial Number/?RS:FS}'

I know printf is printing two strings, .. but are you searching for "Serial Number" again? Also what does ?RS:FS do? Record Separater:Field Seperator? Not sure what the statement means.
 
/Serial Number/?RS:FS is using the C-style ternary operator, which is a shorthand way of saying if (something is true) substitute this, otherwise this. The syntax is expr ? true_value : false_value. So in that case if the input line contained "Serial Number", then the output line would be terminated by a record separator, otherwise by a field separator.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top