How to escape "*" using awk inside script?

Hi , I have a file with the below contents like serialno, userid and password. When am trying to fetch column 3 using awk its working except for the user id jij due to the password having "*" in between. How to retrieve the password.

cat testfile
1 abc bnmj134
2 fff u7Tdff
3 jij Qm6Pn*w
a=`grep jij testfile|awk '{print $3}'`

echo $a --> not giving output and showing error as echo : No match
Note: for other user id giving the correct output and if i remove the "*" from the password its retrieving Qm6Pnw .

you probably have a file named Qm6Pnw in the direfctory you're running these commands in.
try:

a="$(awk '$2=="jij" {print $3}' testfile)"
echo "$a"

actually the column 2 value is not constant and it will vary based on different ids, so cant check with that particular id . Need to get the value if i fetched the userid and corresponding password

Hi, i tried in bash mode:

cat testfile | awk '{print $2 " - " $3}'

Output:

abc - bnmj134
fff - u7Tdff
jij - Qm6Pn*w

What bash version are you using?

ok, still no need for grep .

val2search='jij'

a="$(awk -v v2s="${val2search}" '$2==v2s {print $3}' testfile)"
echo "$a"

it's not a matterof the shell version. It's a matter of using double-quotes when doing var assignment and (more importantly) when echo-ing the variable value.
See my initial response with colored double-quotes.

Also there's no need to use cat - awk is quite happy with reading the supplied file to read.

am using csh

well, my condolences :wink:
you can adopt the ksh/bash approach to csh as well...

I asked about the bash version because rogerben said he got an error, which doesn't happen to me with the same code
To assign the value to a variable I use the backtick and it works. Taking up your code

a=`awk '$2=="jij" {print $3}' testfile`
echo "$a"

I know, I just replaced the grep of the rogerben code with cat

am trying the below command but getting illegal variable in csh.

set a="$(awk -v v2s="${uid}" '$2==v2s {print $3}' testfile)"  -- where uid is the value of the user
echo "$a"

--- Post updated at 11:48 AM ---

if i replaced with other special characters its working fine only its not retrieving the value if there is a "*" in between the word.

I don't know what "other special characters" means and I am not csh-savvy, but the following seem to work

#!/bin/csh

set uid = 'jij'
set a = `awk -v v2s="${uid}" '$2==v2s {print $3}' testfile`
echo "a->$a"

In a standard shell you do not need extra "quotes" around $( ) or ` ` in an assignment like a=... but if you do it works okay.
In contrast, in csh an evaluation/substitution occurs even in an assignment, so you should try

set a="`awk -v v2s="${uid}" '$2==v2s {print $3}' testfile`"

I have seen bugs when there are too many quoting types nested - a recent tcsh should have most of them fixed.
And: the csh parser as such is buggy by design - even the latest tcsh is bug-compatible - for mission-critical please use a standard shell!

For all shell types: wrap a $expression in "quotes" when it appears in command arguments, for example the echo command:

echo "$a"
2 Likes
cat testfile | grep jij | awk -F" " '{print $3}'