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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pass shell parameters to awk does not work 1

Status
Not open for further replies.

kristo5747

Programmer
Mar 16, 2011
41
0
0
US
Why does this work

Code:
    for myfile in `find . -name "R*VER" -mtime +1`
    do
       SHELLVAR=`grep ^err $myfile || echo "No error"`
       ECHO $SHELLVAR
    done

and outputs

Code:
    No error
    err ->BIST Login Fail 3922 err 
    No error
    err ->IR Remote Key 1 3310 err

But this does not

Code:
    for myfile in `find . -name "R*VER" -mtime +1`
    do
       SHELLVAR=`grep ^err $myfile || echo "No error"`
       awk -v awkvar=${SHELLVAR} '{print awkvar}'
    done

and outputs

Code:
    awk: cmd. line:1: fatal: cannot open file `{print awkvar}' for reading (No such file or directory)

What am I missing?



 
This should fix it.

Code:
awk -v awkvar=[red]"[/red]"${SHELLVAR}[red]"[/red] ' ... '

${SHELLVAR} expands to multiple words, the second of which awk assumes is your script. Quoting it forces it to be treated as one parameter to the variable assignment.

Annihilannic.
 
I managed an extra double quote in there somehow... please ignore it.

Annihilannic.
 
I changed to a while loop and fed $myfile to the awk command. It worked. Thank you for your time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top