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:           SubnetMask:
  Multicast Specific:
    Multicast Capability: Yes
    Group             RefCnt      SrcFltMd
    -----             ------      --------         0000000001  Exclude
      SrcAddr: None         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.

  • I take it Perl is no option?
    – sr_
    Commented Aug 8, 2012 at 11:30
  • No Perl I'm afraid Commented Aug 8, 2012 at 11:31

1 Answer 1


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


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.

You must log in to answer this question.

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