I am defining the following variables inside a bash script (note: I ultimately want to pass these values to the bash script from the commandline but I haven't gotten that far yet):

OFS=','
INPUTFILE='~/data.txt'
OUTPUTFILE='~/delimited.txt'
FIELDWIDTHS='1 10 4 2 2'

Next, inside the bash script I'm attempting to call an awk script and pass it these same values:

echo
echo | awk -v FIELDWIDTHS=$FIELDWIDTHS -v OFS=$OFS
 -v INPUTFILE=$INPUTFILE -v OUTPUTFILE=$OUTPUTFILE
 -f ~/bin/fixedWidthToDelimited.awk

The awk script doesn't seem to be able to understand correctly what's being passed to it, I keep getting fatal errors. Can someone please help me understand how to do this? Thanks!

Recommended Answers

All 2 Replies

It would be better to let the shell expand the tilda by removing the quotes or use $HOME with double quotes, because awk probably doesn't expand a tilda.

Quote the variable references:

awk -v FIELDWIDTHS="$FIELDWIDTHS" -v OFS="$OFS" -v INPUTFILE="$INPUTFILE" -v OUTPUTFILE="$OUTPUTFILE"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.