Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning an external field ($1, $2) variable to awk

Status
Not open for further replies.

madasafish

Technical User
Jul 18, 2006
78
TH
I have several files with dates in different fields and would like to assign an external variable "$datefield" different awk fields ie: $1 $2...etc..

#!/bin/bash
starttxt=DATASTART
endtxt=DATAEND
filetype=adf
filedir=$workdir/bin
paramfile=$workdir/param.adf.dat
datefield='"$3"'

for pfile in `ls -1 $filedir/*.$filetype`
do
gawk -v file="$pfile" \
-v start="$starttxt" \
-v end="$endtxt" \
-v datefield="$datefield" \
'{
if ($1 == start)
{
gsub(/"/,"")
printf "%-60s %-20s", file, datefield
}..................etc

I would like awk to see the line..
printf "%-60s %-20s", file, datefield
as
printf "%-60s %-20s", file, $3

Any help appreciated,
Many thanks,
Madasafish

 
Try this:
...
datefield=[!]3[/!]
...
printf "%-60s %-20s", file, [!]$[/!]datefield
...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
As always, perfect, I'm sure I tried all permutations except that one.

Thank-you

Madasafish
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top