Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a bash script:

#!/bin/bash

gawk -f realmap.awk realmap.log | column -ts: > realmap.csv

gnuplot <<-_EOF_
    set term png
    set out 'realmap.png'
    set xlabel 'index'
    set ylabel 'bytes'
    set style data lp
    plot 'realmap.csv' u 1:2 t col, '' u 1:3 t col, '' u 1:4 t col, '' u 1:5 t col, '' u 1:6 t col, '' u 1:7 t col
_EOF_

rm realmap.csv

display realmap.png

And a awk script:

#!/usr/bin/gawk -f

BEGIN{
    printf("%s:%s:%s:%s:%s:%s:%s\n", "index", "total", "used", "free", "cached", "buffers", "cache")
}

/^#/{
    gsub("#", "")
    printf("%d:", $0+1)
}

/^M/{
    printf("%d:%d:%d:%d:", $2,$3,$4,$7)
}

/^-/{
    printf("%d:%d\n", $3, $4)
}

How to combine these two scripts as one?

share|improve this question
    
what do u mean by combine??? Combination depends on what functionality u want to achieve... –  Vineet Menon Dec 7 '11 at 5:30

2 Answers 2

up vote 1 down vote accepted

You don't need a "here document". Just place the entire awk program — properly quoted so that all of the quotation marks and metacharacters within it are not recognized by the shell and so that the linefeeds and other whitespace don't cause it to be split into multiple arguments by the shell — as the first command-line argument, without using awk's -f option. Without -f, the first command line argument is the program to run. The awk manual page is your friend.

share|improve this answer

What you asked for was a heredoc. It tricky to use in this case, but I love heredocs so I'm going to show you have to do it. You have to incorporate an even lesser known bash feature, process substitution with <()

#!/bin/bash

  # The <( begins a process substitution. It's valid to use with -f because what gets
  # substituted is a file descriptor like /dev/fd/5
  # The quoting on '_EOF_' prevents the shell from expanding the contents of the heredoc,
  # as if it were a big quoted string. So, your $2, $3, etc. are safe.
gawk -f <(cat - <<-'_EOF_'
    BEGIN{
        printf("%s:%s:%s:%s:%s:%s:%s\n", "index", "total", "used", "free", "cached", "buffers", "cache")
    }

    /^#/{
        gsub("#", "")
        printf("%d:", $0+1)
    }

    /^M/{
        printf("%d:%d:%d:%d:", $2,$3,$4,$7)
    }

    /^-/{
        printf("%d:%d\n", $3, $4)
    }
_EOF_
) realmap.log | column -ts: > realmap.csv

gnuplot <<-_EOF_
    set term png
    set out 'realmap.png'
    set xlabel 'index'
    set ylabel 'bytes'
    set style data lp
    plot 'realmap.csv' u 1:2 t col, '' u 1:3 t col, '' u 1:4 t col, '' u 1:5 t col, '' u 1:6 t col, '' u 1:7 t col
_EOF_

rm realmap.csv

display realmap.png

So that is the answer you asked for. Now, the way I would do it is with what I call the self consuming script pattern.

#!/bin/bash

  # The <( begins a process substitution. It's valid to use with -f because what gets
  # substituted is a file descriptor like /dev/fd/5
  # Notice the use of brackets. That prevent the following line from matching itself.
gawk -f <(sed -e '/[B]EGIN_AWK1/,/[E]ND_AWK1/!d' $0) realmap.log | column -ts: > realmap.csv

gnuplot <<-_EOF_
    set term png
    set out 'realmap.png'
    set xlabel 'index'
    set ylabel 'bytes'
    set style data lp
    plot 'realmap.csv' u 1:2 t col, '' u 1:3 t col, '' u 1:4 t col, '' u 1:5 t col, '' u 1:6 t col, '' u 1:7 t col
_EOF_

rm realmap.csv

display realmap.png

#BEGIN_AWK1
    BEGIN{
        printf("%s:%s:%s:%s:%s:%s:%s\n", "index", "total", "used", "free", "cached", "buffers", "cache")
    }

    /^#/{
        gsub("#", "")
        printf("%d:", $0+1)
    }

    /^M/{
        printf("%d:%d:%d:%d:", $2,$3,$4,$7)
    }

    /^-/{
        printf("%d:%d\n", $3, $4)
    }
#END_AWK1

To me that is pretty easy to follow and you can put multiple AWK or and other scripts in one file by incrementing the delimiter.

Enjoy bashing! Feel free to come vist #bash on freenode for even quicker answers.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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