[HPADM] SUMMARY: awk question

From: Hustert Klaus-CKH035 (Klaus.Hustert@motorola.com)
Date: Tue Aug 19 2003 - 03:40:44 EDT


Hi folks,
Thanks a lot to all for such amount of helpfully answers!

#----------------------------------------------------------------------
bpmedialist | awk ' !NF == 0 && !/Server Host =/ && !/images/ && !/---------/ { print $0 }' | \
    awk '
        { if ( (NR % 2 ) == 0 )
              printf"%s%s\n", prev_line,$0
          else
              prev_line = $0" " }

Thx 2 my colleague Jim McDonald ( I did not know about him before ;-). Hi Jim.

#----------------------------------------------------------------------
Assuming you have the input in file /tmp/in
The following command will give you every line _before_ the line that ends with "FROZEN"

sed -n '/FROZEN$/{;g;p;}
h;' /tmp/in

(I like sed ;-)

Thx 2 Harm

#----------------------------------------------------------------------

Long_command | sed -n '/FROZEN/{
x
p
x
p
}
h'

Thx 2 Alan

#----------------------------------------------------------------------
I suggest you use the 'available_media' script in the /usr/openv/netbackup/bin/goodies directory.
Thx 2 Tim

#----------------------------------------------------------------------
Try this code:

        BEGIN {
          # train awk to use multi-line records
        RS=""
        FS="\n"
        }

        /FROZEN/ {
          # since whole "line" is now a "field", we will have
          # to break-up field one on white space to get the id
          split($1,junk," ")
          print junk[1]
          }

Thx 2 David

#----------------------------------------------------------------------
Here the answers and on bottom my question:

awk '{if(substr($1,1,2) == "BE") { id=$1; getline ; if($NF == "FROZEN") print id}}'

This will print only the ID if you have FROZEN as the status. If you want the whole line rather than just the ID, then replace the "id = $1" with "id = $0". If you want both lines if the status is frozen, do as mentioned above, but also instead of "print id" od "print $0".

Thx 2 Vince
#----------------------------------------------------------------------

In awk, you can set the end-of-line to be a blank line, and then the fields will be numbered from 1 - N between blank lines:

##
# joanna:~stuart/awk/patsup.awk
#
#
# Variables:
# ARGC Argument Count
# ARGV Array of command line arguments
# FILENAME Input filename
# FNR Current record number
# FS Field seperator
# NF Numver of fields
# NR Current record number
# OFS Output field Spererator
# ORS oupt record seperaotr
# RS (input) record seperator
# $1 First field
# $0 entire line
BEGIN {
        RS = "" # set record seperator to blank line
        }
/^BE0/ {
        if ($13 == "FROZEN") print $0
        }
END {
        print "Done!"
        }

Something like that...

Thx 2 Stuart

#----------------------------------------------------------------------

/usr/openv/netbackup/bin/admincmd/bpmedialist | awk ' BEGIN { ###Print the heading lines if ($1 == "id")
    print $0
if ($1 == "vimages")
    print $0
if (substr($1,1,5) == "-----")
    print $0

###Save the BE line for later pringting.
if (substr($1,1,2) == "BE")
    line_one = $0

###Print the lines
if ($4 == "FROZEN") {
    print line_one
    print $0
}
}'

Thx 2 Ronelle

#----------------------------------------------------------------------
awk '! /FROZEN/ {l=$*} /FROZEN/ {print $l; print $*}'

this will print all lines containing FROZEN and the immediately preceeding one ("l" acts as a buffer)

you could also try the GNU version of grep which allows you to print context lines

grep -B1 FROZEN

which says : print all lines containing FROZEN and one line before

Thx 2 Thierry
#----------------------------------------------------------------------

bash-2.03# /usr/openv/netbackup/bin/admincmd/bpmedialist | awk '$1 ~ /BE[0-9][0-9]*/ {
 getline
 if($NF == "FROZEN") print}'
}'

Thx 2 Sundar

#----------------------------------------------------------------------

 awk '{a=$0;getline; b=$0;if ( b ~ /FROZEN/) {print a; print b;}; getline}' a.txt

Thx 2 balaji

--
             ---> Please post QUESTIONS and SUMMARIES only!! <---
        To subscribe/unsubscribe to this list, contact majordomo@dutchworks.nl
       Name: hpux-admin@dutchworks.nl     Owner: owner-hpux-admin@dutchworks.nl
 
 Archives:  ftp.dutchworks.nl:/pub/digests/hpux-admin       (FTP, browse only)
            http://www.dutchworks.nl/htbin/hpsysadmin   (Web, browse & search)


This archive was generated by hypermail 2.1.7 : Sat Apr 12 2008 - 11:02:32 EDT