2

I would like to replace a variable inside the the awk command with a bash variable.

For example:

var="one two three"    
echo $var | awk "{print $2}"

I want to replace the $2 with the var variable. I have tried awk -v as well as something like awk "{ print ${$wordnum} } to no avail.

3 Answers 3

5

Sightly different approach:

$ echo $var
one two three

$ field=3

$ echo $var | awk -v f="$field" '{print $f}'
three

$ field=2

$ echo $var | awk -v f="$field" '{print $f}'
two
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for the right approach but double-quote your shell variables.
@EdMorton - I intentionally left the quotes out since all the OP needed was a number describing which field to print. But, yes, you are correct .-)
The rule of thumb is "always quote your variables unless you have a specific reason not to" and by a specific reason I mean some behavior you want to exhibit that you MUST leave your variables unquoted to produce. Quoting should be your default behavior to avoid surprises down the road. Even if you know the dangers of laving variable unquoted and think it's safe here, the people reading this may not and might think what you're showing them here would work for their application when it could be a time bomb for them.
2

You've almost got it...

$ myfield='$3'
$ echo $var | awk "{print $myfield}"
three

The hard quotes on the first line prevent interpretation of $3 by the shell. The soft quotes on the second line allow variable replacement.

8 Comments

that makes sense! I need to pass $2, not the value of $2. Thanks.
except that I need to pass the variable from the shell. e.g. silo.sh something 5, so that $1 = something and $2 = 5, thus passing $5 to print the 5th item from the awk command.
Do not do this as it results in bizarre errors when your shell variable contains some characters. See cfajohnson.com/shell/cus-faq-2.html#Q24 for the right way to pass the value of shell variables to awk scripts. There are other problems with just the shell part of this answer too.
@EdMorton , are there more advanced linux tips tuts like that from which i can learn more
They're like SO in as much as you can ask questions and get answers but it's where all of the shell and awk experts hang out so the answers you get will be rock solid and you can learn a ton by browsing the archives. It's where I go to ask questions. You can use your email reader (e.g. Thunderbird) to connect to a news server (e.g. eternal-september.org) and read/write news postings just like you do email after subscribing to whatever newsgroups you're interested in. Google groups also has a half-baked interface to netnews and there's webnews.net too, both for access from browsers.
|
0

You can concatenate parts of awk statements with variables. Maybe this is what you want in your script file:

echo $1|awk '{print($'$2');}'

Here the parts {print($ and the value of local variable $2 and );} are concatenated and given to awk.

EDIT: After some advice rather don't use this. Maybe as a one-time solution. It's better to get accustomed to doing it right right away - see link in first comment.

3 Comments

Do not do this as it results in bizarre errors when your shell variable contains some characters. See cfajohnson.com/shell/cus-faq-2.html#Q24 for the right way to pass the value of shell variables to awk scripts. There are other problems with just the shell part of this answer too.
@EdMorton: Could you give examples for both problems? If I'm in control of input is there still a reason not to use concatenated statements?
Try it with $1 and/or $2 equal to * or $2 unpopulated or set to a non-numeric value or .... The best reason not to use concatenated statements is probably that there is no reason TO use concatenated statements and if you always do it the right way then you don't trip over yourself using it the wrong way by mistake some time when you're not as in control of the input as you thought. Ditto for quoting your shell variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.