0
#!/bin/ksh
Temp=Kerala
cs1=`grep "$Temp" State_list.txt | awk '{print $NF}'`
echo $cs1 #("Not seeing output KL":

If I change the code as follows, it works:

cs1=`grep "Kerala" State_list.txt | awk '{print $NF}'`

... but I would like to pass it as variable.

The file State_List.txt is as follows:

Karnataka KAR
TamilNadu TN
Kerala    KL

migrated from serverfault.com Feb 21 '18 at 13:44

This question came from our site for system and network administrators.

  • Bash or ksh the shebang says #!/bin/ksh? – fedorqui Feb 21 '18 at 13:41
  • 1
    That looks like it should work (with ksh, or bash, or dash). Make sure you don't have any typos there, or weird invisible characters. If the data file has windows style CRLF line endings, it shouldn't matter, but if the script file does, you'll assign Kerala\r to the variable, and you won't be able to find that in the file. But then you might also get an error when running the script, since the hashbang would be wrong. – ilkkachu Feb 21 '18 at 13:56
  • 1
    @natrajeee20, yes. How do you run the script? Did you check the line endings? – ilkkachu Feb 21 '18 at 14:19
  • 1
    @natrajeee20, well, now that you've ignored my other point twice, I could mention it a third time, or just leave you to it. – ilkkachu Feb 21 '18 at 14:28
  • 1
    @natrajeee20 The issue is with possible CRLF line endings in the script. If there is an extra \r at the end of each line, then $Temp will not be Kerala but Kerala\r, which is why it doesn't match in the indata file. Changing the indata file form DOS text to Unix text would obviously also be a good thing to do. – Kusalananda Feb 21 '18 at 14:54
3

The issue was solved in the comment section.

Summary:

The files, both the script and possibly also the data file, were DOS text files. On Unix, that means that they had a superfluous \r (carriage return) at the end of each line.

For the script, this meant that the variable assignment

Temp=Kerala

assigned the value Kerala\r to the variable Temp. Since the data file did not contain this value, it was never found.

Running the script with the value inserted in place of the variable worked since it, at that place in the data, was not affected by the DOS text issue.

The utility dos2unix should be installed and run on both files to make sure that they are Unix text files. See also other questions on this site relating to this utility.


The script could also have been written like this:

#!/bin/sh

Temp='Kerala'
cs1=$( awk -v pattern="$Temp" '$1 ~ pattern { print $NF }' State_list.txt )
printf '%s\n' "$cs1"

This gets rid of the grep and uses awk in its place to scan for any line in the in-data file's first column that contains pattern (a variable assigned to $Temp on the command line). With == in place of ~ an exact string matching is performed rather than a regular expression match.

The script is an sh script since it (and the original) does not use any special ksh features.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.