Morris Fauntleroy
Morris Fauntleroy

Reputation: 246

awk - how to "re-awk" the output?

I need to take a file and count the number of occurrences of $7 - I've done this with awk (because I need to run this through more awk)

What I want to do is combine this into one script - so far I have

#! /usr/bin/awk -f
# get the filename, count the number of occurs
# <no occurs> <filename>
{ print $7 | "grep /datasheets/ | sort | uniq -c"}

how do I grab that output and run it through more awk commands - in the same file

Eventually, I need to be able to run

./process.awk <filename> 

so it can be a drop-in replacement for a previous setup which would take too much time/effor to to change -

Upvotes: 2

Views: 454

Answers (2)

user1461760
user1461760

Reputation:

If you use Gawk, you could use the 2-way communications to push the data to the external command then read it back:

#!/usr/bin/gawk -f

BEGIN {
  COMMAND = "sort | uniq -c"
  SEEN = 0
  PROCINFO[ COMMAND, "pty" ] = 1
}

/datasheets/ {
  print $7 |& COMMAND
  SEEN = 1
}

END {
  # Don't read sort output if no input was provided
  if ( SEEN == 1 ) {

    # Tell sort no more input data is available
    close( COMMAND, "to" )

    # Read the sorted data
    while( ( COMMAND |& getline SORTED ) > 0 ) {
      # Do whatever you want on the sorted data
      print SORTED
    }
    close( COMMAND, "from" )
  }
}

See https://www.gnu.org/software/gawk/manual/gawk.html#Two_002dway-I_002fO

Upvotes: 1

Kent
Kent

Reputation: 195209

if you want to forward the output of an awk script to another awk script, just pipe it to awk.

awk 'foobar...' file|awk 'new awkcmd'

and your current awk|grep|sort|uniq could be done with awk itself. save your 3 processes. you want to get the repeated counts, don't you?

awk '$7~=/datasheets/{a[$7]++;} END{for(x in a)print x": "a[x]' file

should work.

Upvotes: 3

Related Questions