LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-16-2009, 11:23 AM   #1
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Question importing bash variable into awk script = AARGH!!


Code:
awk -v var="A" '{ printf("%c",int($var)); } {print}'
Could someone kindly tell me how to make the above code work?? I expect a "65" to be returned

I know the answer looks obvious (probably) and maybe it is, but please understand, I have been working on this since last night, and unless I have overlooked the ONE of the dozens of permutations of this, that actually works, I'm STUMPED

I believe I have tried virtually all combinations of escaping, slashes, backslashes, quotes, double-quotes, ........ ARGH!! (Did I already yell that?) ARGH!!

PS - I do NOT want to `echo` the $var through a pipe into `awk` -- that works, but I do not want that here.

Here is a TINY sample of the typical results:

Code:
fatal: 0 is invalid as number of arguments for int                                                                                                                              
/                                                                                                                                                                                      
bash-4.0$ awk -v var="A" '{ printf("%c",int($var)); } {print}'                                                                                                                         
^C                                                                                                                                                                                     
bash-4.0$ awk -v var="A" '{ printf("%c",int(var)); } {print}'                                                                                                                         
^C                                                                                                                                                                                    
bash-4.0$ awk -v var="A" '{ printf("%c",int(\$var)); } {print}'                                                                                                                        
awk: { printf("%c",int(\$var)); } {print}                                                                                                                                              
awk:                   ^ backslash not last character on line                                                                                                                          
bash-4.0$ awk -v var="A" '{ printf("%c",int(\$var\)); } {print}'                                                                                                                       
awk: { printf("%C",int(\$var\)); } {print}                                                                                                                                             
awk:                   ^ backslash not last character on line                                                                                                                          
bash-4.0$ awk -v var="A" '{ printf("%c",int(\$var)); } {print}\'                                                                                                                      
awk: { printf("%c",int(\$var)); } {print}\                                                                                                                                            
awk:                   ^ backslash not last character on line                                                                                                                         
bash-4.0$ awk -v var="A" '{ printf("%c",int(\$var/)); } {print}'                                                                                                                      
awk: { printf("%C",int(\$var/)); } {print}
awk:                   ^ backslash not last character on line
bash-4.0$ awk -v var="A" '{ printf("%c",int($var)); } {print}'
^C
bash-4.0$ awk -v var="A" '{ printf("%d",int($var)); } {print}'
^C
bash-4.0$ awk -v var="A" '{ printf("%d",int(var)); } {print}'
^C
bash-4.0$ awk -v var="A" '{ printf("%d",int('var')); } {print}'
^C
bash-4.0$ awk -v var="A" '{ printf("%d",int('$var')); } {print}'
awk: fatal: 0 is invalid as number of arguments for int
bash-4.0$ awk -v var="A" '{ printf("%d",int('"$var"')); } {print}'
awk: fatal: 0 is invalid as number of arguments for int
bash-4.0$ awk -v var="A" '{ printf("%d",int('"\$var"')); } {print}'
^C
bash-4.0$ awk -v var="A" '{ printf("%d",int(\'"$var"')); } {print}'
awk: { printf("%d",int(\)); } {print}
awk:                   ^ backslash not last character on line
bash-4.0$ echo $(awk -v var="A" '{ printf("%d",int($var)); } {print}')
^C
bash-4.0$ echo $(awk -v var="A" '{ printf("%d",int(\$var)); } {print}')
awk: { printf("%d",int(\$var)); } {print}
awk:                   ^ backslash not last character on line
Thank you
Sasha

Last edited by GrapefruiTgirl; 09-16-2009 at 11:25 AM.
 
Old 09-16-2009, 12:06 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
In awk, printf is not a function, but a statement. So no parenthesis with printf in awk. (awkward side-note: sprintf() on the other hand is a function).

Also, no $ to reference the variable. So this works:
Code:
awk -v var="42" 'BEGIN {printf "... and everything = %d\n", strtonum(var)}'
From the bad-news-department, there is no function in awk that takes a character and converts to its ascii-integer. But it can be implemented in awk-code itself if that part of your question is important to you. Read all about that here.

Last edited by Hko; 09-16-2009 at 12:07 PM.
 
Old 09-16-2009, 12:13 PM   #3
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Ok, I will address your reply specifically in a little while, but first, I wonder if you would consider the following, which does work:

Code:
bash-4.0$ echo $(printf %d "'$(echo "A" | awk '{ printf("%c",int($0)); } {print}')")
65
bash-4.0$
If the above works, can you differentiate between why THAT works, and why I cannot do it without using the `echo` functionality?

Further, observe the following, which hangs up, and does not work (I just substituted the variable directly into the int() operation, instead of echoing it through):

Code:
bash-4.0$ echo $(printf %d "'$(awk '{ printf("%c",int("A")); } {print}')")
^C
bash-4.0$
And, thank you very much for your reply -- again, I will read the link you provided (though I likely came across it during the past 24 hours) and I will reply further afterwards.

Sasha

Last edited by GrapefruiTgirl; 09-16-2009 at 12:16 PM.
 
Old 09-16-2009, 12:56 PM   #4
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'll be honest and say I don't fully understand the command (I take it you're trying to print the decimal value of the ascii character), but if I gather what's going on correctly...
Code:
echo $(printf %d "'$(echo "A" | awk '{ printf("%c",int($0)); } {print}')")
The first printf is the bash built-in, so it's what's giving you the final value, not awk's printf. Awk is really doing nothing at all in the above string.

Also, just to point it out, since bash's printf can basically be considered an advanced echo, you're really just duplicating commands. You can just use printf alone and get the same result:

Code:
printf %d "'$(echo "A" | awk '{ printf("%c",int($0)); } {print}')"
Or even just...
Code:
printf %d "'A"
Since "A" is all the nested code is producing.

(But can someone tell me what the extra ' means here?)
 
Old 09-16-2009, 01:04 PM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
LMAO

My goodness -- thank you DavidTheH -- If I had known that the nested code wasn't doing anything, and that I could have simply done:

Code:
printf %d "'A"
..then this whole situation would have been solved many days ago! You win a year supply of the "San Fransisco Treat"

Wow.. that makes things so much less convoluted -- I'll point you to the end goal of this ordeal later on via a link here. You may be perplexed as to why I'm putting so much time into this, but that's ok: sometimes we like being perplexed.

Thank you,
Sasha
 
Old 09-16-2009, 01:21 PM   #6
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
Mmm. Rice-a-Roni. Haven't had that in years. LOL

Glad to help out, although I mostly just broke it down and ran the commands individually to see what they produced. Truthfully, I still don't really understand printf; it's such a complex, cryptic command and there's not a lot of clear beginner's info on it that I've been able to find. And my understanding of ascii manipulation is almost as limited.
 
Old 09-16-2009, 01:24 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
<edit>
Oh, I wasn't the first one to reply anymore :-)
Serves me right I guess, for typing such long posts...
</edit>

Quote:
Originally Posted by GrapefruiTgirl View Post
Ok, I will address your reply specifically in a little while, but first, I wonder if you would consider the following, which does work:

Code:
bash-4.0$ echo $(printf %d "'$(echo "A" | awk '{ printf("%c",int($0)); } {print}')")
65
bash-4.0$
If the above works, can you differentiate between why THAT works, and why I cannot do it without using the `echo` functionality?
Wow, that was a confusing one.. are you testing me? :-P

Splitting it up in its parts, I found that it is equivalent to just this:
Code:
bash$ printf %d "'A"
65
(except for the lack of a newline after 65)

In other words, it is the shell utility printf in your command that does the trick of converting 'A' to its ascii-code number. Note that the single single quote is needed to do that trick. I was surprised about this, but I found it documented in the info page about the GNU shell utility printf ("info coreutils printf". not in its man page).

On the awk part of your command line, the printf part does not do anything at all! Because int($0) evaluates to int("A") and the string "A" does not contain any digits. (note that what int() wants to do is this kind of things: int("123") --> 123, not returning the ascii-code of a character or string...).

Also I now noticed that the syntax of printf with parenthesis is accepted and works, though the man page of awk says it is a statement and show printf without parenthesis. So this may be a GNU-awk-only feature.

It is only the "{print}" part that just outputs what comes in through the pipe. So the awk part of the command line is equivalent to just:
Code:
bash$ echo "A" | awk '{print}'
A
In summary:
Code:
# Your command line:
echo $(printf %d "'$(echo "A" | awk '{ printf("%c",int($0)); } {print}')")

# ...is just like this:
echo $(printf %d "'$(echo "A" | awk '{print}')")

# ...which is the same as:
echo $(printf %d "'$(echo "A")")

# ...which is nothing more than:
echo $(printf %d "'A")

# ...and finally (except that now it does not end the output with a newline):
printf %d "'A"

# which is just the shell utility printf and has nothing to do with awk...

Quote:
Further, observe the following, which hangs up, and does not work (I just substituted the variable directly into the int() operation, instead of echoing it through):
Code:
bash-4.0$ echo $(printf %d "'$(awk '{ printf("%c",int("A")); } {print}')")
^C
bash-4.0$
Well, in this case you're just making bash choke in the 3 single-quotes you feed it.

Simplified, it is the same as this, which also "hangs":
Code:
printf %d "'$(awk '{print}')"
The single quote problem is here:
Code:
printf %d "                 "
           '$(awk '{print}')
           ^      ^       ^
           |      |       |
           1      2       3
Quote:
And, thank you very much for your reply
You're welcome. I learned something here too.

Last edited by Hko; 09-16-2009 at 01:29 PM.
 
Old 09-16-2009, 01:25 PM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Quote:
Originally Posted by David the H. View Post
And my understanding of ascii manipulation is almost as limited.
I'm with you on all counts, but I gotta say, regarding ascii manipulation -- there seems to be scores of ways to do the manipulations I want, but most of them involve more or less easy/complicated ways of using languages other than bash & its friends.

Since my programming knowledge + ability is not particularly great wrt C+family, I tend to try doing most of what I want, using bash. Generally where there's a will, there's a way but evidently I overlooked this most simple of items: "'A"

Thanks again!
Sasha

Last edited by GrapefruiTgirl; 09-16-2009 at 01:29 PM.
 
Old 09-16-2009, 01:28 PM   #9
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
and @ HKo -- nope, it wasn't merely a test -- rather, a long way of going about proving that I missed that most obvious & simple functionality pointed out by David & yourself: the "'A" method!

I am glad that you feel you learned something as well -- my utter confusion does have a silver lining in this case

Sasha
 
Old 09-16-2009, 01:41 PM   #10
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
I actually think that programming C, PHP, or Perl is easier than programming bash, and I don't think that just because of familiarity with those others. I write bash scripts when I have to, but invariably I find myself involved in tedious debugging of arcane and non-forgiving syntax rules. The syntax isn't consistent; you have to resort to complicated commands with their own syntax (awk, sed...and was that printf in awk or in bash...) for many things, and you are at the mercy of your environment; if some commands are not available, then your script won't work.

Against that, the other languages I listed have regular syntax and standardized libraries. Perl can bite you with all its extensions, but PHP is pretty standard unless you get into some arcane things, and C is quite standard across platforms.
 
Old 09-16-2009, 01:46 PM   #11
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
Thanks here too for the detailed post Hko. It made a few things clearer for me too, especially using ' to specify the numeric value of the following character.

But yeah, dontcha just hate it when you spend all that time researching and typing, only to find that four other people have answered the question before you could hit print?
 
Old 09-16-2009, 03:00 PM   #12
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
http://www.linuxquestions.org/questi...74#post3685674

See there for what I used your most graciously provided help for

Sasha
 
Old 09-16-2009, 05:46 PM   #13
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Wow.. Did you find the cipher yourself?
 
Old 09-16-2009, 05:51 PM   #14
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Heh, if I understand your question, then no, I wasn't the first to bring up this funny thing on LQ, nor was I the first to solve it, though I believe I was the first here to post the decoded version in English.

In that thread though, I and others have been trying to inspire anyone interested, to provide a means of decoding it using whatever programming languages they are able to do it with, so if you have a favorite language that hasn't been used yet, by all means, provide us your decoder

Sasha
 
Old 09-17-2009, 01:47 AM   #15
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by GrapefruiTgirl View Post
http://www.linuxquestions.org/questi...74#post3685674

See there for what I used your most graciously provided help for

Sasha
That is what I figured you were up to
 
  


Reply

Tags
awk, bash, import, variable



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
[BASH] Hangman Game: changing a character within a variable without use of sed or awk uzi85 Linux - Newbie 1 03-27-2009 01:41 AM
Using global variable in awk (bash script) kopeda Programming 2 04-24-2007 01:47 AM
Bash Awk Variable problem _hadi_ Programming 5 12-13-2006 12:25 AM
cannot export result from awk into a variable in a bash script Emmanuel_uk Linux - Newbie 4 03-07-2005 01:54 AM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 07:47 AM

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

All times are GMT -5. The time now is 11:58 PM.

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