Alex Brandt
Alex Brandt

Reputation: 99

Can awk accept two arguments?

I'm new to this and a little in the dark, so if my title is off the mark please correct me. I'm trying to set a variable in awk from one file, and then invoke the script on a different file.

ex:

I execute:

cat textfile | ./awkscript

awkscript pulls 'fields' var from fields.txt while running on textfile

Here is what I have. I'm using getline, and that isn't what I'm looking for. I want it to grab the value from the first line of a separate file.

\#!/opt/local/bin/gawk -f

BEGIN { 
    printf "Enter field lengths: "
    getline fields < "-"
        print fields
}       

BEGIN {FIELDWIDTHS = fields; OFS="|"}
{
        {       for (i=1;i<=NF;i++) sub(/[ \t]*$/,"",$i)        }

\#       {       for (i=1;i<=NF;i++) sub(/^[ \t]*/,"",$i)        }

        print
}

Upvotes: 0

Views: 73

Answers (1)

Alex Brandt
Alex Brandt

Reputation: 99

What I was looking for was this:

cat textfile | generic.awk -v fields='10 1 21 21 4'

The -v option can also be used multiple times: cat textfile | generic.awk -v field1="10" -v field2="1" -v field3="21" -v field4="21" -v field5="4"

Upvotes: 1

Related Questions