0

I want to get a user intput at the begining af the script (as an argument) and then compare it to a string using awk ! I'm using -v to add the variable into the awk command(by the way the input is STRING) But for some reason it won't show the expected output ! And i know that the problem is on the variable that i inserted into the awk command , because if i put instead of the variable the string that i want to find, the specific one that i know that is inside the file , it will find it and print the result i want. I will show you code !

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

i have also tried:

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

But nothing. I cannot find the solution. A clarification here. I have made the correct seperation and i know the file what it includes!

And here an example that works without the variable

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

psz is the string that i have set before the awk command at the a variable ( a="psz" ) like that. And it is what i know that is inside the /etc/passwd at the first field ($1) !!

Panagiss
  • 3,154
  • 2
  • 20
  • 34
  • Kindly go through the link mentioned in your thread now, you need to only fix the procedure that how to call shell variables in `awk` then you should be good – RavinderSingh13 Nov 27 '19 at 17:49

1 Answers1

1

You should use instead of this:

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

this:

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

You define x as a variable and this should be used in awk

Romeo Ninov
  • 6,538
  • 1
  • 22
  • 31