Find the answer to your Linux question:
Results 1 to 5 of 5
Hi all, I am working as computational chemist and regularly use bash scripting to simplify my work. I have been using grep command successfully to extract data from log files ...
  1. #1
    Just Joined!
    Join Date
    Nov 2008
    Posts
    4

    Problem using variable as pattern in grep

    Hi all,

    I am working as computational chemist and regularly use bash scripting to simplify my work. I have been using grep command successfully to extract data from log files of different softwares. However, today i came across a weired problem working with grep

    Here is what i am trying to do
    1. store the path to a file1 in variable "file1"
    2. get basename of file2
    3. match basename of file2 with an integer (in file3) and save it in another variable "x"
    4. get the line containing "x" from file1

    Following code does not work and there is no output from the last grep command

    Code:
    file1="/path/to/file"
    bname=`basename ${file2} .csv
    x=`grep -w "${bname}" file3 | awk -F "," '{print $3"}'`
    grep -w "${x}" ${file1}
    I would like to mention the if i manually save the integer value in variable "x", then the last command works
    Code:
    x=532540
    grep -w "${x}" ${file1}
    
    output:532540,104.45,342391442.00,
    Please suggest me where i am going wrong. any help in this matter will be of great importance to me.

    Thanking you all in advance

  2. #2
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    119
    Quote Originally Posted by johnsmithgr8 View Post
    Hi all,

    I am working as computational chemist and regularly use bash scripting to simplify my work. I have been using grep command successfully to extract data from log files of different softwares. However, today i came across a weired problem working with grep

    Here is what i am trying to do
    1. store the path to a file1 in variable "file1"
    2. get basename of file2
    3. match basename of file2 with an integer (in file3) and save it in another variable "x"
    4. get the line containing "x" from file1

    Following code does not work and there is no output from the last grep command

    Code:
    file1="/path/to/file"
    bname=`basename ${file2} .csv
    x=`grep -w "${bname}" file3 | awk -F "," '{print $3"}'`
    grep -w "${x}" ${file1}
    I would like to mention the if i manually save the integer value in variable "x", then the last command works
    Code:
    x=532540
    grep -w "${x}" ${file1}
    
    output:532540,104.45,342391442.00,
    Please suggest me where i am going wrong. any help in this matter will be of great importance to me.

    Thanking you all in advance

    Without knowing the content of the files to test, adn assuming that what you have typed in the code section is exactly what you are trying to do, I would say you have an extra quote after the $3 in the awk statement. Other than that, it looks fine.

    Code:
    file1="/path/to/file"
    bname=`basename ${file2} .csv
    x=`grep -w "${bname}" file3 | awk -F "," '{print $3}'`
    grep -w "${x}" ${file1}

  3. #3
    Linux User Manko10's Avatar
    Join Date
    Sep 2010
    Posts
    250
    There are some syntax errors in the skript. I would also suggest using the parenthesis operator instead of backticks for command substitution. See if that works:
    Code:
    file1="/path/to/file"
    bname=`basename ${file2} .csv`
    x=$(grep -w "${bname}" file3 | awk -F "," '{print "$3"}')
    grep -w "${x}" ${file1}
    To give you further advice we need to know the contents of your file.
    Refining Linux Advent calendar: “24 Outstanding ZSH Gems

  4. #4
    Just Joined!
    Join Date
    Nov 2008
    Posts
    4
    Dear all,

    thank you very much for your suggestions and pointing out the mistake in code.
    however, after making the corrections also, it was not working

    finally trying to find some workaround, i came to know that it was file3 which was the source of problem. Since content of the file3 are not going to be modified, i exchanged column2 (string) with column3 (integer)., and then tried the following

    Code:
    file1="/path/to/file"
    bname=`basename ${file2} .csv
    x=`grep -w "${bname}" file3 | awk -F "," '{print $2}'`
    grep -w "${x}" ${file1}
    in the above case, variable "x" contains integer value, and it works.

    however, minor modification of the above code (required in another script) extracts the data contained in column 3 (string) of the file3.

    Code:
    file1="/path/to/file"
    bname=`basename ${file2} .csv
    y=`grep -w "${bname}" file3 | awk -F "," '{print $3}'`
    echo "y:$y"
    
    expected output: y:hartree
    output: y:hartree?
    in this case, variable "y" contains the expected string value, but it is always appended by a question mark (?).

    I guess, the problem was may be with end line character. so I added a fourth column in file3 containing all integer zero values. then both the above code works fine. Although, i have the code working, i would like to know the reason for such behavior, so that i can take care of it in future.

    Once again than you for your support and suggestions

    john

  5. #5
    Just Joined!
    Join Date
    Jun 2010
    Posts
    6

    Maybe some hidden char?

    Windows EOL? You can use "cat -A file" to show hidden characters and EOL stuff. I got bit by Windows EOL just yesterday.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...