0

I have a file that that contains output of some command.

# cat input.txt

*******************************************************************************
deinstall PREVIEW:  deinstall operation will not actually occur.
*******************************************************************************

+-----------------------------------------------------------------------------+
                    Pre-deinstall Verification...
+-----------------------------------------------------------------------------+
Verifying selections...done
Verifying requisites...done
Results...

FAILURES
--------
  Filesets listed in this section failed pre-deinstall verification
  and will not be removed.

  Dependency Failures
  (Deinstall Operation)
  ---------------------
  SELECTED FILESETS:  The following is a list of filesets that you asked to
  remove.  They cannot be removed until all of their dependent filesets
  are also removed.  See subsequent lists for details of dependents.

    my.data.list 6.1.2.0         # This is my data list...

  INSTALLED DEPENDENTS:  The following filesets are dependents of one or more
  of the selected filesets listed above.  These must be removed before
  or with the filesets that you selected.  To remove these dependents with
  the selected filesets, specify the option to automatically remove dependent
  software (-g flag).

    apple.fruit 6.1.9.200            # This is apple...
    ball.object 6.1.9.200             # This is ball 
    bat.object 6.1.9.200              # This is bat
    cat.animal 6.1.9.200              # this is cat 
    nut.object 6.1.9.200              # this is nut 
    hut.house 6.1.9.200              # this is hut 

  << End of Failure Section >>

FILESET STATISTICS 
------------------
    1  Selected to be deinstalled, of which:
        1  FAILED pre-deinstall verification
  ----
    0  Total to be deinstalled


******************************************************************************
End of deinstall.  No deinstall operation has actually occurred.
******************************************************************************

I am trying to get/print the list of installed dependents

apple.fruit 6.1.9.200            # This is apple...
ball.object 6.1.9.200             # This is ball 
bat.object 6.1.9.200              # This is bat
cat.animal 6.1.9.200              # this is cat 
nut.object 6.1.9.200              # this is nut 
hut.house 6.1.9.200              # this is hut 

I have gone through this How to select lines between two patterns? link. And has come up with this command but it is not working.

What am i doing wrong here?

awk '/"software (-g flag)."/ f; /"<< End of Failure Section >>"/{f=0}' input.txt
0

7 Answers 7

3

Never just say it is not working, whether it's when asking for help to debug software or taking your car to the mechanic or dropping off your dog at the veterinarians. Always describe the symptoms of the item in question "not working" to have the best chance of getting help, e.g. wrong output, no output, core dump, error messages, something else (some of those don't apply in the dog case :-) ).

Having said that:

  1. the start/end lines in your file are not surrounded by double quotes so don't ask your script to match on them, and
  2. (, ) and . are regexp metacharacters so if you want them treated literally you have to escape them.

Try this:

awk '/software \(-g flag\)\./ f; /<< End of Failure Section >>/{f=0}' input.txt

and then modify it to this:

awk '/software \(-g flag\)\./ f && NF; /<< End of Failure Section >>/{exit}' input.txt

to get rid of the blank lines and for efficiency.

2

@Albert: Try:

awk '/End of Failure Section/{A=""} /software \(-g flag\)/{A=1;next} A && !/^$/{print}'   Input_file

Explanation: Checking if a line is having string (software (-g flag)) if yes then making a variable named A to TRUE, if a line is having string (End of Failure Section) then make variable named A's value to 0. then check if variable A's value is TRUE/ONE and line is NOT NULL then print the current line.

1
$ awk '
       /software \(-g flag\)/{ f=1; next }
       /<< End of Failure Section >>/{f=0}
       f && NF
      ' file

Explanation

$ awk '                                       # call awk
       /software \(-g flag\)/{ f=1; next }    # look for regex in record, if found set var f=1 and got to next line
       /<< End of Failure Section >>/{f=0}    # look for regex if found then set var f to 0
       f && NF                                # if f and NF then print record, this is to skip empty lines
      ' file
1

Here's another solution with awk:

awk '/INSTALLED DEP/{a=1} a&&/^$/{b=1;a=0;next} b{print} b&&/^$/{b=0}' File

    apple.fruit 6.1.9.200            # This is apple...
    ball.object 6.1.9.200             # This is ball
    bat.object 6.1.9.200              # This is bat
    cat.animal 6.1.9.200              # this is cat
    nut.object 6.1.9.200              # this is nut
    hut.house 6.1.9.200              # this is hut
1

Because there is a sed tag:

sed -n '/software (-g flag)/,/<< End of Failure Section/{//!p}' input.txt
1
sed '/software (-g flag)./,/End of Failure Section/!d;//d' input.txt
0

Another way would use regex matching to print out records containing desired patterns:

$ awk '/([a-z][. ])+([0-9].?)+/' file
    my.data.list 6.1.2.0         # This is my data list...
    apple.fruit 6.1.9.200            # This is apple...
    ball.object 6.1.9.200             # This is ball 
    bat.object 6.1.9.200              # This is bat
    cat.animal 6.1.9.200              # this is cat 
    nut.object 6.1.9.200              # this is nut 
    hut.house 6.1.9.200              # this is hut 

Regex above is no means perfect, for example the first outputed record should not be there and should be weeded out adding for example && !/my./ to the condition:

$ awk '/([a-z][. ])+([0-9].?)+/ && !/my./' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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