Question about reading the output of a command line by line

Question about reading the output of a command line by line

Post by Drew Simoni » Tue, 09 May 2000 04:00:00



All,
I need to get certain elements of the output of
`pkginfo -l` on a Solaris system.

Using Korn Shell, is there any syntax similar to the following
Perl snippet:

open( PKGINFO, "/bin/pkginfo -l |" ); # creates a filehandle

  while( chop( $line = <PKGINFO> ) ) {
    if ( index( $line, "   PKGINST:  " ) == 0 ) {
      $pkg = substr( $line, 13 );
    } elsif( index( $line, "  CATEGORY:  " ) == 0 ) {
      $cat = substr( $line, 13 );
    } elsif( index( $line, "   VERSION:  " ) == 0 ) {
      $ver = substr( $line, 13 );
      printf( "%-13s %-22s %s\n", $pkg, $cat, $ver );
    }
  }

For whatever reason, I need to use shell instead of Perl, and
converting sequences like the above have proven difficult at
best.  

I have considered redirecting stdout to a file, then using
sed or awk to get the bits of info I need, but that seems
shabby.  Am I missing something?  

TIA for any insight

 
 
 

Question about reading the output of a command line by line

Post by Barry Margoli » Tue, 09 May 2000 04:00:00




Quote:>All,
>I need to get certain elements of the output of
>`pkginfo -l` on a Solaris system.

>Using Korn Shell, is there any syntax similar to the following
>Perl snippet:

>open( PKGINFO, "/bin/pkginfo -l |" ); # creates a filehandle

>  while( chop( $line = <PKGINFO> ) ) {
>    if ( index( $line, "   PKGINST:  " ) == 0 ) {
>      $pkg = substr( $line, 13 );
>    } elsif( index( $line, "  CATEGORY:  " ) == 0 ) {
>      $cat = substr( $line, 13 );
>    } elsif( index( $line, "   VERSION:  " ) == 0 ) {
>      $ver = substr( $line, 13 );
>      printf( "%-13s %-22s %s\n", $pkg, $cat, $ver );
>    }
>  }

pkginfo -l | while read line
do
  ...
done

Quote:>I have considered redirecting stdout to a file, then using
>sed or awk to get the bits of info I need, but that seems
>shabby.  Am I missing something?  

Yes, you've forgotten about pipes!

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Question about reading the output of a command line by line

Post by Cyrille Lefevr » Wed, 10 May 2000 04:00:00



> All, I need to get certain elements of the output of `pkginfo -l` on a
> Solaris system.

> Using Korn Shell, is there any syntax similar to the following Perl
> snippet:

> open( PKGINFO, "/bin/pkginfo -l |" ); # creates a filehandle

>   while( chop( $line = <PKGINFO> ) ) {
>     if ( index( $line, " PKGINST: " ) == 0 ) {
>       $pkg = substr( $line, 13 );
>     } elsif( index( $line, " CATEGORY: " ) == 0 ) {
>       $cat = substr( $line, 13 );
>     } elsif( index( $line, " VERSION: " ) == 0 ) {
>       $ver = substr( $line, 13 );
>       printf( "%-13s %-22s %s\n", $pkg, $cat, $ver );
>     }
>   }

in ksh, you can use co-processes like this :

pkginfo -l |&
while read -p line rest; do
        case $line in
        PKGINST:)
                pkg=$rest ;;
        CATEGORY:)
                cat=$rest ;;
        VERSION:)
                ver=$rest
                printf "%-13s %-22s %s\n" $pkg $cat $ver ;;
        esac
done

Cyrille.
--


 
 
 

Question about reading the output of a command line by line

Post by Drew Simoni » Wed, 10 May 2000 04:00:00



> Yes, you've forgotten about pipes!

Im ashamed to admit that I never knew about pipes with shell
scripting in this way.  (im learning as I go, just bought a
book last week =)
 
 
 

Question about reading the output of a command line by line

Post by Drew Simoni » Wed, 10 May 2000 04:00:00



> in ksh, you can use co-processes like this :

> pkginfo -l |&
> while read -p line rest; do
>         case $line in
>         PKGINST:)
>                 pkg=$rest ;;
>         CATEGORY:)
>                 cat=$rest ;;
>         VERSION:)
>                 ver=$rest
>                 printf "%-13s %-22s %s\n" $pkg $cat $ver ;;
>         esac
> done

This does just the trick!  Thanks so much.
 
 
 

1. awk-outputting input line in specified output line

Hello,

    I have a question about awk that I have asked a number of people
about but have had no joy with. A colleague have mine has suggested that
I submit the question to this newsgroup to see if anyone can help me.

Basically is there a way that I can use awk to extract a line from an
input file and put it into an output file on a specific line number i.e.

Input file contents

Hello there how are you
today. I am fine thank
you

If I use awk '/today/' input_file to extract the line today. I am fine
thank

Is there a way that I can output the line 'today. I am fine thank' on
for instance line 5 in the output file?

Output file contents

blank line
blank line
blank line
blank line
today. I am fine thank

I would appreciate any help with this.

Thanks

Ian

2. SIGILL why does it happen ????

3. line by line reading of the lines in list

4. Keyboard commands in Openwindows/Linux

5. line by line without using echo "$i" | while read line?

6. PKIX

7. look for a better way to read command line output

8. ppptp

9. read from stdin while reading line by line a file

10. Can a script output a command to the Command line as though typed

11. awk-outputting input lines on specified line number

12. Linux permanent Line (connection two linux machines over a leased line (permanent telephone line)

13. one liner which prints n lines before and m lines after the line found by grep