0

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 the 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.

Beat
  • 4,386
  • 2
  • 35
  • 52
Ishan
  • 29
  • 3
  • 2
    `cut -d "|" -f1-4 <<< '102|Sam|*|USA'` works for me. Can't reproduce the problem. – hek2mgl Jan 13 '16 at 12:44
  • On what OS? Works on OS X Mavericks – Brian Jan 13 '16 at 12:51
  • Possible duplicate of [When to wrap quotes around a variable](http://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-variable) – tripleee Jan 13 '16 at 13:08
  • 3
    The problem is that you need `echo "$tgt_val"` with quotes instead of `echo $tgt_val`. As an aside, your code is not correcly transcribed; you need `tgt_val=$(...)` to capture the output into the variable (what you have is a syntax error, unless the value of `$file_name` happens to be the name of a valid command). – tripleee Jan 13 '16 at 13:10
  • Thanks @tripleee that worked. It was a problem with my $tgt_val. It worked when i echo'ed the value as echo "$tgt_val" – Ishan Jan 13 '16 at 13:56

1 Answers1

0

It was a problem with my $tgt_val. It worked when i echo'ed the value as echo "$tgt_val"

Ishan
  • 29
  • 3