LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-25-2010, 07:42 AM   #16
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195

Quote:
Originally Posted by GrapefruiTgirl View Post
I thought declare was optional
It is pretty much; I don't think there's a single declare in all my shellscripts; a few "readonly"s and lots of "local"s but no "declare"s.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 11-25-2010, 08:32 AM   #17
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by GrapefruiTgirl View Post
I tried doing this:
Code:
awk '$1!=""{printf("%s","dataarray[\""$1"\"]=\""$2"\";")}END{print}' test
which produces this:
Code:
dataarray["kill"]="bill";dataarray["epic"]="deal";dataarray["blonde"]="chick";dataarray["linux"]="questions";
which you can eval:
Code:
root@reactor: eval $(awk '$1!=""{printf("%s","dataarray[\""$1"\"]=\""$2"\";")}END{print}' test)
Executes without error; however, the array does not seem to be created properly:
Code:
root@reactor: echo ${dataarray[*]}
questions
root@reactor:
And in any event, even if this works after someone points out what I'm doing wrong, I agree with catkin - there's probably an easier way to either use just awk, or just bash. IF not, that's fine, but please humor us: can you give us a larger view of the entire process requirements here? I.e. you want to process this file into an array in awk (and presumably do something to the data) but then you also want the same array, in bash. I think there must be a compromise or different perspective that maybe can be taken, but to suggest, we need to know what's going on in the larger picture.
Thanks a lot But I don't really know why you included END{print} in the code.
Code:
awk '$1!=""{printf("%s","dataarray[\""$1"\"]=\""$2"\";")}END{print}' test
I get the following output when i use that
Code:
dataarray["kill"]="bill";dataarray["epic"]="deal";dataarray["blonde"]="chick";dataarray["linux"]="questions";linux questions
So I removed END{print} part and it seems to be working fine. Not sure whether that was a typo or some trick beyond my knowledge.
Anyway many thanks to you again and also to catkin. His code might take a while to digest.
 
Old 11-25-2010, 08:45 AM   #18
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
But I don't really know why you included END{print} in the code
I used the final print statement after all records have been processed, because up to that point, the code is only using printf, which by default does not output a newline. The final print command only served to output a newline at the end of the string of variable declarations. Possibly it isn't needed in a non-interactive terminal, but it isn't hurting anything, except in your case where it is printing stuff that it shouldn't be printing.

You have shown me that the code as written, evidently does not work the exact same on your system as it does on mine. On yours, it appears that the final print statement is printing the current/last record, even though this print statement is in the END section and is intended to output just a newline. On my machine, it prints just a newline. Here's a comparison:
Code:
root@reactor: awk '$1!=""{printf("%s","dataarray[\""$1"\"]=\""$2"\";")}END{print "\n"}' test 
dataarray["kill"]="bill";dataarray["epic"]="deal";dataarray["blonde"]="chick";dataarray["linux"]="questions";

root@reactor: awk '$1!=""{printf("%s","dataarray[\""$1"\"]=\""$2"\";")}END{print}' test
dataarray["kill"]="bill";dataarray["epic"]="deal";dataarray["blonde"]="chick";dataarray["linux"]="questions";
root@reactor:
so you see, they are the same for me (no extra junk at the end of the line; just a newline) - and if you wanted, you could use the first version which includes the "\n" if you needed to. But, if it works fine for you without an END section, then just leave it out.

For the record, below shows why I put the END section (run in an interactive terminal):
Code:
root@reactor: awk '$1!=""{printf("%s","dataarray[\""$1"\"]=\""$2"\";")}' test
<--snip some out for readability-->\
dataarray["blonde"]="chick";dataarray["linux"]="questions";root@reactor:
See at the end of the output line, how there's no newline, and my prompt is returned to me right at the end of the line. Yuck!

Good luck! Keep us posted, this is interesting stuff.

Last edited by GrapefruiTgirl; 11-25-2010 at 08:48 AM.
 
Old 11-25-2010, 09:15 AM   #19
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by GrapefruiTgirl View Post
I used the final print statement after all records have been processed, because up to that point, the code is only using printf, which by default does not output a newline. The final print command only served to output a newline at the end of the string of variable declarations. Possibly it isn't needed in a non-interactive terminal, but it isn't hurting anything, except in your case where it is printing stuff that it shouldn't be printing.

You have shown me that the code as written, evidently does not work the exact same on your system as it does on mine. On yours, it appears that the final print statement is printing the current/last record, even though this print statement is in the END section and is intended to output just a newline. On my machine, it prints just a newline. Here's a comparison:
Code:
root@reactor: awk '$1!=""{printf("%s","dataarray[\""$1"\"]=\""$2"\";")}END{print "\n"}' test 
dataarray["kill"]="bill";dataarray["epic"]="deal";dataarray["blonde"]="chick";dataarray["linux"]="questions";

root@reactor: awk '$1!=""{printf("%s","dataarray[\""$1"\"]=\""$2"\";")}END{print}' test
dataarray["kill"]="bill";dataarray["epic"]="deal";dataarray["blonde"]="chick";dataarray["linux"]="questions";
root@reactor:
so you see, they are the same for me (no extra junk at the end of the line; just a newline) - and if you wanted, you could use the first version which includes the "\n" if you needed to. But, if it works fine for you without an END section, then just leave it out.

For the record, below shows why I put the END section (run in an interactive terminal):
Code:
root@reactor: awk '$1!=""{printf("%s","dataarray[\""$1"\"]=\""$2"\";")}' test
<--snip some out for readability-->\
dataarray["blonde"]="chick";dataarray["linux"]="questions";root@reactor:
See at the end of the output line, how there's no newline, and my prompt is returned to me right at the end of the line. Yuck!

Good luck! Keep us posted, this is interesting stuff.

I see. The classic "different systems have buffers" case. Thanks for the explanation greatly appreciate it.
 
Old 11-25-2010, 09:34 AM   #20
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,493

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
@GGirl - I am curious if you have (g)awk installed ro maybe one of the derivatives like mawk or nawk?

I only ask as I get the same behaviour which is the default as per the man and web pages:
Quote:
Originally Posted by man
print Prints the current record. The output record is terminated with the value of the ORS variable.
Quote:
Originally Posted by web
The simple statement ‘print’ with no items is equivalent to ‘print $0’: it prints the entire current record.
web
 
Old 11-25-2010, 09:59 AM   #21
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
@ grail, yes I'm using gawk; regardless, what I *should* have done, is this (from the webpage linked by you - thank you):
Code:
To print a blank line, use ‘print ""
Instead, I just expected there to be no implicit reference made to $0 by the plain print statement since we were in the END section, and just get a newline; from the gawk man page:
Code:
BEGIN and END are two special kinds of patterns which are not tested against the input....
all the END blocks are merged, and executed when all the input is exhausted
Maybe I read too much into that? Maybe I've just been lucky till now. See what trouble I get into?!

I'm also curious, what OS and *awk version the OP is using. If I recall, another of OP's threads may have gotten into a difference-between-versions discussion too, but I'm not sure...

OP, what *awk are you using, and what OS? For what it's worth, in the future, it may help to alleviate version related confusion (if that's what this is) if you mention which OS you're using, especially in the case of it being something like Windows, cygwin, another strange/uncommon setup, or non-GNU software.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Help needed for using awk to parse a file to make array for bash script tallmtt Programming 12 04-14-2012 01:16 PM
Bash + Reading values(numbers) from a file and storing them into an array SuchANewb Linux - Newbie 5 11-04-2010 08:11 AM
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
Awk output to bash array? kj6loh Programming 4 09-07-2009 12:36 PM


All times are GMT -5. The time now is 12:40 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration