-1

I am trying to create a shell script, below is a part of which where i am trying to extract some value from a record in a file:

tgt_val=`cat $file_name | grep "$string_name" | cut -d"|" -f$column_no`

This work perfectly when the specified column has some numeric or string value however it fails when th column has value as " * "

eg:

102|Sam|*|USA

This command would work correctly for column 1,2 & 4

However in case of column 3 it gives me output as all the files in present directory.

Someone please help over this.

don_crissti
  • 79,330
  • 30
  • 216
  • 245
Ishan
  • 1
  • 1
  • 3
    [Quote your variables!](http://unix.stackexchange.com/q/131766). Use `printf '%s\n' "$tgt_val"`, not `echo $tgt_val` (same for those `$file_name` and `$column_no` that you forgot to quote). See also [Security implications of forgetting to quote a variable in bash/POSIX shells](http://unix.stackexchange.com/q/171346) – Stéphane Chazelas Jan 13 '16 at 12:37

1 Answers1

1

What is happening is that somehow the * is being interpreted by the shell. Make sure all extracted fields are always properly quoted.

vonbrand
  • 18,156
  • 2
  • 37
  • 59