1

I am trying to insert the output below into the variable x. The output is a string. I have done this before.

k="psz"

And when I do this it works and i get the expected output when doing echo $x

x=$( awk -v a="k" -F '[:,]' '{ if($1 == "psz")  print $5 }' /etc/passwd )

But when i try to use this one below it doesn't work

x=$( awk -v a="k" -F '[:,]' '{ if($1 == a)  print $5 }' /etc/passwd )

It does not work, echo $x gives me a blank line.

5
  • 1
    In the second one k isn't defined before if($1 == k), you mean if($1 == "k")? or if($1 == a)? Commented Nov 27, 2019 at 19:37
  • 1
    The test should be if ($1 == a) not k, and if you want a to be set with the value of k, then you should pass -v a=$k instead. Commented Nov 27, 2019 at 19:37
  • yea yea you are right! It's a not k. it's a i made a typo. Commented Nov 27, 2019 at 19:42
  • the problem still exists ! Commented Nov 27, 2019 at 19:43
  • 2
    Check my answer. I guess you're still passing -va=k instead of -va=$k, the right form. Commented Nov 27, 2019 at 19:53

1 Answer 1

3

You are setting a with the string k and not the value of variable $k. If you set it right, the code will work fine. Look:

k='accdias'
x=$(awk -va=$k 'BEGIN{FS=":"} $1==a {print $5}' /etc/passwd)
echo $x
Antonio Dias

I'm editing this to show another way of passing variable values to your awk program without using -v:

k='accdias'
x=$(awk 'BEGIN{FS=":"} $1==ARGV[2] {print $5}' /etc/passwd $k)
echo $x
Antonio Dias

On the above code ARGV[0] will be set to awk, ARGV[1] will be set to /etc/passwd, and finally ARGV[2] will be set to $k value, which is accdias on that example.


Edits from Ed Morton (see comments below):

k='accdias'
x=$(awk -v a="$k" 'BEGIN{FS=":"} $1==a {print $5}' /etc/passwd)
echo "$x"
Antonio Dias

k='accdias'
x=$(awk 'BEGIN{FS=":"; a=ARGV[2]; ARGV[2]=""; ARGC--} $1==a {print $5}' /etc/passwd "$k")
echo "$x"
Antonio Dias
Sign up to request clarification or add additional context in comments.

7 Comments

yea you are right i found it. Do you know how i can put an if else into the awk?
you have the right idea for if/else in your OP. Just { if($1 == a) {print $5} else {print "no match"} } . Good luck.
No worries. About the if/else, something like this will do it if (condition){commands} else {commands}.
You must quote your shell variables (see mywiki.wooledge.org/Quotes) and if you don't set ARGV[2] to null after reading it then awks going to try to open accdias like an input file, see stackoverflow.com/q/19075671/1745001. Also if you don't put a space after the -v (i.e. -v a="$k") then it makes the script unnecessarily gawk-only.
@EdMorton, you're absolutely right on all those (as always!). Thanks for the heads up and, if you don't mind, could you please fix the answer? I don't know how to do it. :-)
|

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.