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 ...
- 12-17-2010 #1Just 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
I would like to mention the if i manually save the integer value in variable "x", then the last command worksCode:file1="/path/to/file" bname=`basename ${file2} .csv x=`grep -w "${bname}" file3 | awk -F "," '{print $3"}'` grep -w "${x}" ${file1}
Please suggest me where i am going wrong. any help in this matter will be of great importance to me.Code:x=532540 grep -w "${x}" ${file1} output:532540,104.45,342391442.00,
Thanking you all in advance
- 12-17-2010 #2Linux Newbie
- Join Date
- Apr 2007
- Posts
- 119
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}
- 12-17-2010 #3
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:
To give you further advice we need to know the contents of your file.Code:file1="/path/to/file" bname=`basename ${file2} .csv` x=$(grep -w "${bname}" file3 | awk -F "," '{print "$3"}') grep -w "${x}" ${file1}Refining Linux Advent calendar: “24 Outstanding ZSH Gems”
- 12-18-2010 #4Just 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
in the above case, variable "x" contains integer value, and it works.Code:file1="/path/to/file" bname=`basename ${file2} .csv x=`grep -w "${bname}" file3 | awk -F "," '{print $2}'` grep -w "${x}" ${file1}
however, minor modification of the above code (required in another script) extracts the data contained in column 3 (string) of the file3.
in this case, variable "y" contains the expected string value, but it is always appended by a question mark (?).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?
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
- 12-24-2010 #5Just 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.


Reply With Quote
