Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have some netstat output I'm scripting and want to parse. What's the most efficient way of parsing values from the output below? It's important to point out that I'm using the old /bin/sh shell and not bash

I'm generating this output from the command: netstat -d -K O30S1O1

MVS TCP/IP NETSTAT CS V1R11       TCPIP Name: NCDTCP          10:12:39
DevName: OSAQO130          DevType: MPCIPA
  DevStatus: Ready         CfgRouter: Non  ActRouter: Non
  LnkName: O30S1O1           LnkType: IPAQENET    LnkStatus: Ready
    Speed: 0000001000
    IpBroadcastCapability: No
    ArpOffload: Yes                ArpOffloadInfo: Yes
    ActMtu: 8992
    VLANid: None                   VLANpriority: Disabled
    ReadStorage: GLOBAL (4096K)    InbPerf: Balanced
    ChecksumOffload: Yes           SegmentationOffload: Yes
    SecClass: 255                  MonSysplex: No
  Routing Parameters:
    MTU Size: 1500              Metric: 10
    DestAddr: 0.0.0.0           SubnetMask: 255.255.255.192
  Multicast Specific:
    Multicast Capability: Yes
    Group             RefCnt      SrcFltMd
    -----             ------      --------
    224.0.0.5         0000000001  Exclude
      SrcAddr: None
    224.0.0.1         0000000001  Exclude
      SrcAddr: None
  Link Statistics:
    BytesIn                           = 13545386
    Inbound Packets                   = 129630
    Inbound Packets In Error          = 0
    Inbound Packets Discarded         = 0
    Inbound Packets With No Protocol  = 0
    BytesOut                          = 81424019
    Outbound Packets                  = 520188
    Outbound Packets In Error         = 0
    Outbound Packets Discarded        = 0


IPv4 LAN Group Summary
LanGroup: 00006
  Name              Status      ArpOwner          VipaOwner
  ----              ------      --------          ---------
  O30S1O1           Active      O30S1O1           Yes

For example, let's say I want to fetch several values such as DevName, BytesIn and BytesOut. I plan on running the netstat command every minute or so on four interfaces and writing the output to a csv file for importing to excel.

share|improve this question
    
I take it Perl is no option? –  sr_ Aug 8 '12 at 11:30
    
No Perl I'm afraid –  conorgriffin Aug 8 '12 at 11:31
add comment

1 Answer

up vote 3 down vote accepted

Whatever you parse it with, I recommend outputting in this format:

DevName=OSAQO130
BytesIn=13545386

because that way you can enclose the script in eval $(...) and get ready to use variables $DevName and $BytesIn.

Parsing with perl:

netstat -d -K O30S1O1 | perl -ne '$DevName = $1 if m/DevName: (\S+)/; $BytesIn = $1 if m/BytesIn.*= (\d+)/; END {print map("$_=${$_}\n", qw/DevName BytesIn/)}'

Parsing with sed:

netstat -d -K O30S1O1 | sed -ne 's/DevName: \([^ ]*\).*/DevName=\1/ p' -e 's/.*BytesIn.*= \([0-9]*\)/BytesIn=\1/ p'

In Windows I use the bash shell that comes with git, it has a lot of unix tools including both sed and perl.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.