[SOLVED] importing bash variable into awk script = AARGH!!
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
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.
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):
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.
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...
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?)
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.
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.
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):
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.
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
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.
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?
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.