ExtractCmd.sh (Source)

#!/bin/sh -e
######################################################################
# ExtractCmd.sh
#
#  Copyright (c) 2016, Russell Adams <Russell.Adams@AdamsSystems.nl>
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are
#  met:
#
#   - Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#
#   - Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the
#     distribution.
#
#   - Neither the name of Russell Adams, Adams Systems Consultancy,
#     nor the names of its contributors may be used to endorse or promote
#     products derived from this software without specific prior written
#     permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ***** Requires GAWK to be installed. See below for details. *****
#
# This script uses awk to pull a command's output from an already
# untarred IBM AIX snap file. The output is in a format suitable for
# piping to grep for further analysis.
#
# For example:
#
#  Place a series of untarred snap files in your working directory,
#  with each snap untarred into an individually named subdirectory,
#  like:
#
#      ./host1.snap/
#      ./host2.snap/
#
#  Then to lookup a common command like 'lsattr -El sys0' use:
#
#      $ $PATH/ExtractCmd.sh 'lsattr -El sys0'
#
#  This returns:
#
#      host1.snap:     .....
#      host1.snap:     .....    bootinfo -K
#      host1.snap:     .....
#      host1.snap:
#      host1.snap:     32
#      host1.snap:
#      host1.snap:     .....
#      host2.snap:     .....    bootinfo -K
#      host2.snap:     .....
#      host2.snap:
#      host2.snap:     64
#      host2.snap:
#      host1.snap:     .....
#      host3.snap:     .....    bootinfo -K
#      host3.snap:     .....
#      host3.snap:
#      host3.snap:     64
#      host3.snap:
#
# Regular expressions are used to locate the commands, so you can use
# regexp to specify one command or a series of commands, like:
#
#  Return all attributes on EVERY hdisk on the system, including
#  hdiskpower devices:
#
#      lsattr -El hdisk
#
#  Return only ent1 attributes, noting that ent1 could match ent10:
#
#      lsattr -El ent1\$
#
# require a command
[ -z "$1" ] && {
    echo "Specify a quoted command as the first parameter"
    exit -1
}
CMD="$1"
# Each hostname.snap directory has subdirectories underneath with a .snap file in it
# We search those only for command output
for SNAP in *.snap ; do
    gawk "BEGIN {RS=\"\n\n.....\n\"} /${CMD}/ { print \"\n.....\n\" \$0 }" ${SNAP}/*/*.snap \
        | gawk "{printf \"%-20s %s\n\", \"${SNAP}:\", \$0 }"
done