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

Using shell variables in awk statement??? 2

Status
Not open for further replies.

RobJordan

MIS
Apr 3, 2001
296
US
This is from a k shell script.

echo " $LINE\c"|awk '{printf("%-20s",$0)}' # print menu item

As you can see, in the second line of code,
I am trying to make the statement dynamic
by replacing the hard coded 20 with a shell variable.

echo " $LINE\c"|awk '{printf("%-${MENU_ITEM_WIDTH}s",$0)}'

Anyone know the correct syntax?

Thanks in advance,

Robert
Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
Hi Robert:

There was a thread over in the awk forum about this several months ago. There's two ways of handling this:

1) Surround your shell variable in the awk script with double quotes.

2) pass a variable to the awk script:

echo $1| nawk ' {
if ($0 ~ svalue)
print 1
else
print 0 } ' svalue="$2"

I think this is what you're asking.

Regards,

Ed
 
Easy examples

-v command line option

awk -v SEEK="$MYSTRING" '$0 ~ SEEK' "$@"

and as mentioned above
embedded example (IMHO the best)

awk '/'"$MYSTRING"'/ {print}' "$@"

Pseudo-file example

awk '$0 ~ SEEK' SEEK="$MYSTRING" "$@"

Cheers,
ND
 
Thanks for your help!
Here's the final solution I got to work.

echo " $LINE\c"|awk -v MIW=${MENU_ITEM_WIDTH} '{printf("%-"MIW"s",$0)}'

Robert

Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top