LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-29-2012, 07:38 AM   #1
haraldboehmecke
LQ Newbie
 
Registered: Nov 2011
Posts: 7

Rep: Reputation: Disabled
gawk printf array


Hi guys,

got stuck on this one:

Code:
gawk 'BEGIN {
printf("|%20s|%50s|%20s|\n","Value","Master","Slave");
printf("|%20s|%50s|%20s|\n","Instance", ENVIRON["${MVRRP_INSTANCE[0]}"] ,"sinst");
}'
outputs:
Code:
|               Value|                                            Master|               Slave|
|            Instance|                                                  |               sinst|
array is not being printed. Why?

tried the following printf commands with no luck:

Code:
printf("|%20s|%50s|%20s|\n","Instance",ENVIRON["${MVRRP_INSTANCE[0]}"],"sinst");
printf("|%20s|%50s|%20s|\n","Instance","${MVRRP_INSTANCE[0]}","sinst");
printf("|%20s|%50s|%20s|\n","Instance",${MVRRP_INSTANCE[0]},"sinst");
printf("|%20s|%50s|%20s|\n","Instance",`eval echo ${MVRRP_INSTANCE[0]}`,"sinst");
printf("|%20s|%50s|%20s|\n","Instance","`eval echo ${MVRRP_INSTANCE[0]}`","sinst");

Thanks for your efforts
 
Old 01-29-2012, 08:02 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I guess my question to you would be, where is this variable / array being set? How would you access this value at the command line?
 
Old 01-29-2012, 08:49 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I'm assuming that "${MVRRP_INSTANCE[0]}" is a shell array entry, correct?

Shell variable values need to either be imported into awk variables, or else careful quoting must be used to allow them to expand before the awk command runs. As it stands, the entire gawk expression is enclosed in single-quotes, and no expansions are done.

Read up on the basics here:

http://www.grymoire.com/Unix/Awk.html#uh-4
http://www.gnu.org/software/gawk/man...hell-Variables

Edit: It appears that you're trying to use the ENVIRON array to fetch the value from the current environment. In which case your problem is probably that the shell value needs to be exported in the parent shell first, so that it gets copied to the environment of the gawk sub-process.


But why are you using gawk just to (apparently) print a simple formatted header? Unless you intend to expand the command later to process some kind of input, why not use your shell's built-in printf instead?

Code:
printf "|%20s|%50s|%20s|\n" Value Master Slave
printf "|%20s|%50s|%20s|\n" Instance "${MVRRP_INSTANCE[0]}" sinst

Last edited by David the H.; 01-29-2012 at 09:00 AM. Reason: as stated
 
Old 01-29-2012, 03:41 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Try "echo ${MVRRP_INSTANCE[0]}" | getline MVRRP;, and use the MMRRP variable in your printf statement. (Technically, you should close the pipe, but it will be automatically closed when the gawk program finishes, so it's probably not necessary.)
 
Old 02-01-2012, 04:21 AM   #5
haraldboehmecke
LQ Newbie
 
Registered: Nov 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
thanks to your help I found a solution. The next time I will mention that I am working on a BASH script

KR,

Harald
 
Old 02-01-2012, 05:46 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Remember to provide your solutions so others can learn from you
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] shell script help: copying directory list into an array and then accessing the array richman1234 Linux - Newbie 6 07-25-2010 11:19 PM
[SOLVED] gawk 3.1.3 vs gawk 3.1.1 sharky Programming 2 04-13-2010 01:55 PM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM
gawk help.... visitnag Linux - Newbie 1 04-12-2008 11:55 AM
FS=? in gawk realos Programming 2 05-28-2003 07:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration