1

I have a file with things like serialno, userid and password. When fetching column 3 using awk, a problem crops up for user id jij because of the "*" in the password. How to retrieve the password properly?.

cat testfile
1 abc bnmj134
2 fff u7Tdff
3 jij Qm6Pn*w

a=`grep jij testfile|awk '{print $3}'`

echo $a does not output, showing error as echo : No match.

Vera
  • 1,173
  • 4
  • 17
  • 1
    https://stackoverflow.com/questions/102049/how-do-i-escape-the-wildcard-asterisk-character-in-bash The problem is with echo, not awk. Use double quotes. echo “$a” – HelloWorld Oct 28 '21 at 03:44
  • 1
    You should almost always double-quote variable references. See the stackoverflow question ["I just assigned a variable, but `echo $variable` shows something else"](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) and many similar questions. – Gordon Davisson Oct 28 '21 at 03:57
  • Turn off globbing. – PersianGulf Oct 28 '21 at 04:04
  • How can one turn off globbing inside a function? – Vera Oct 28 '21 at 04:05
  • with `shopt` built-in shell function – PersianGulf Oct 28 '21 at 04:08
  • https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html – PersianGulf Oct 28 '21 at 04:14
  • @PersianGulf, the knob to disable globbing is `set -f`, not `shopt` anything. And disabling globbing globally is a rather big hammer to use. Disabling the error from a non-matching glob is `shopt -u failglob` in Bash though, but letting it glob in the first place is still wrong. – ilkkachu Oct 28 '21 at 05:45
  • @Fatipati, that error message looks a bit suspect. Bash would give the error `bash: no match: Qm6Pn*w` (if `failglob` is set, which it isn't by default), zsh would say `zsh: no matches found: Qm6Pn*w` and _(t)csh_ says `echo: No match.`. Other than the extra space, your error looks like tcsh, but you tagged this with [[tag:bash]], so you may want to check what shell you're running. – ilkkachu Oct 28 '21 at 05:47
  • You could be right because I am trying to adapt some code to my new ubuntu installation. – Vera Oct 28 '21 at 05:54
  • Copy/paste your code into http://shellcheck.net and fix the errors it'll tell you about. It can only catch basic errors like unquoted variables but it's a start. – Ed Morton Oct 28 '21 at 13:33

0 Answers0