Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
You are right in suspecting a quoting problem. When the shell you are issuning the command from constructs the command line in back quotes, it leaves everything between single quotes exactly as it is, including $input_var.
In the form suggested above, the shell you are issuing the command from finds $2== in single quotes so leaves it exactly as it is then finds $input_var outside single quotes so substitutes its value.
Welcome to the subtle world of shell quoting!
You can make life a little easier for yourself by using $( <your command> ) rather than `<your command>`
Are you talking about using a bash variable inside of awk?
The single quotes around the awk expression keep the variable from expanding. Everything within them will be passed to awk as-is. To pass the contents of a bash variable to awk you need to "unquote" it.
(Think of quoting in bash as a toggle switch rather than a container. They turn special character interpretation on and off.)
Assuming that $old_val is the bash variable you want to use:
BTW, You can also set an awk variable to the value of a bash variable with the command option -v before entering the main expressions (you do know that awk has it's own variable system, don't you? ).
Code:
awk -v awkvar="$bashvar" '{awk expression}'
edit: what catkin said.
Last edited by David the H.; 07-01-2009 at 02:25 PM.
Reason: further edit: re-worded for clarity
Distribution: Various, Ubuntu, Fedora, Open Solaris, Solaris, RHEL, CentOS
Posts: 64
Original Poster
Rep:
Thank you very much! I was sure there was some subtlety I was missing and I have to be honest I find the awk man page rather impenetrable but think awk is brilliant.
I also wasn't aware of the separate variables, as I've never used awk for anything terribly complicated before! In fact, I've only ever used it to extract columns of data from large files.
It's for a rather huge shell script I've been working on which splits a mysql database based on certain parameters and basically I need to use a look up table to make sure that each of the secondary keys in the new databases are correctly referenced.
Why shell?! I was given a huge perl script which didn't work and thought shell would be the easiest way for me to make sure I could re-write it correctly as I can test each command - that and I was too lazy to do it in C!
Distribution: Various, Ubuntu, Fedora, Open Solaris, Solaris, RHEL, CentOS
Posts: 64
Original Poster
Rep:
I can see a love hate relationship developing between me and awk. I think I'll be buying a book about it one of these days because it is so useful but so difficult! I think I've seen a 2" thick one!
Thanks for the birthday wishes, I didn't know it showed it anywhere! 29 yesterday - I hate the 9s more than the 0s!
Edit -
I just realised what you meant, I registered on 1st June!
I think I'll be buying a book about it one of these days because it is so useful but so difficult!
Despite its age, the best book for me was "The AWK Programming Language" by Aho, Kernighan and Weinberger known as the little grey book. In some ways it was even a good primer because of the authors' clarity. It has the same feel as K+R's The C Programming Language -- elegantly minimal but complete. If nothing else I grinned every time I saw the authors' names on the cover; you don't have to have a weird name to be a genius computing pioneer but it helps!
Aww, shucks. Someone noticed. I'm touched (and not just in the head).
I recommend the grymoire too. In fact, I only just finished reading through it myself, so I'm kind of an awk newbie too.
Do beware though that the articles are pretty old and there are a lot of typos. They're also mostly focused on old unix versions of the commands; some of the stuff won't apply perfectly to the gnu ones we use. They make good primers though, if you can avoid the hazards.
Try removing the $ from the variable ($2==old_val). I'm still not clear on why (like I said, I'm still new myself), but apparently it isn't needed when calling up the value of variables inside the awk expression. At least not when matching.
Edit, from your last post, it looks like you've figured it out yourself.
Edit2: Oh yes, I remember now. From the grymoire:
Quote:
One more point about the use of a dollar sign. In scripting languages like Perl and the various shells, a dollar sign means the word following is the name of the variable. Awk is different. The dollar sign means that we are refering to a field or column in the current line. When switching between Perl and AWK you must remener that "$" has a different meaning. So the following piece of code prints two "fields" to standard out. The first field printed is the number "5", the second is the fifth field (or column) on the input line.
BEGIN { x=5 }
{ print x, $x}
Last edited by David the H.; 07-02-2009 at 09:48 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.