Scripting question - feed an input file into an if statement line-by-line
Hi all,
I am trying to write a script that takes an input file ($FileName) and an intermediate file ($FileName.info) and removes lines from $FileName if the value in $2 of $FileName.info is <75. I can't figure out how to feed only one line of the .info file to the if statement at a time so that it will perceive it as an integer instead of a list. The error I am getting now is ./script.sh: line 6: [: : integer expression expected Sample input $FileName Code:
>KOG0001|ABCD|Contig4550 Code:
Contig4550 97.440582 Code:
>KOG0001|ABCD|Contig4550 Code:
for FileName in *.fa Thanks! Kevin |
Try removing the double quotes from here:
Code:
if [ "$percent_difference" -gt 75 ] ; then Code:
if [ $percent_difference -gt 75 ] ; then <edit2>Second thought... your values in filename.info appear to be 'real' numbers not integers ... </edit2> cheers |
You'll need to use the bc utility for floating point comparision.
|
Code:
#!/bin/bash Code:
$ ./shell.sh |
Thanks all!
ghostdog, The script works perfectly on the sample input data I posted but when I tried it on real data, it didn't work. Any ideas? The organization of the file (including line breaks in the lines not containing greater-than symbols) is the same. file Code:
>KOG0022|NVEC|245141_1470034 Code:
245141_1470034 91.382767 Kevin Edit: for reasons unknown to me, the spacing in "file" looks different but I think this is a copy/paste error. The file I am using looks fine. I have checked to make sure that it has unix linebreaks (as did the test file). |
if you don't tell me why it doesn't work, how am i going to know?? i ran it with your real data, which you should have provided in the first place, and it runs fine. what errors you got? describe them as much as you can ...
|
Interesting. I thought the data I provided originally would be a good enough proxy for the real deal but evidently not. The script did not return any errors but it didn't remove the first sequence which has a value in the .dist file >75.
Thanks, Kevin |
Quote:
|
Me again...
I tried to modify ghostdog74's script to be iterative for all files in a folder but I'm having some trouble. It works on the first file but it makes no changes to the following files. Any ideas what I'm doing wrong? Code:
#!/bin/bash Kevin |
As I understand it, you are comparing an int to a non-int. bash cannot compare non-int values, use bc as advised above.
Also Code:
Using the [[ ... ]] test construct, rather than [ ... ] can prevent |
Thanks Chris. The script ghostdog wrote truncates the decimal number in the second field of the .info file to the nearest whole number before it is passed to if so I didn't think that was the problem. The double vs. single bracket issue is news to me so I will look into this now. Also, I forgot to mention that I tried kbp and catkin's suggestion of removing the double quotes around the variable in the if statements but this didn't seem to have an effect.
I played around with it a lot this morning and I seem to have it working and behaving properly now. I tried unsetting all of the variables before the declare statement and that seems to have done the trick. Code:
#!/bin/bash Kevin |
All times are GMT -5. The time now is 09:11 AM. |