2

I have some problem with grep. I wrote a script which need to find some numbers from a list (file name ContigsNames) in a database. I wrote the following script:

file=ContigsNames
while IFS=' ' read -r f1 f2
do
    grep '$f1' /data/databases/fasta/lizih/metagenemark_predictions.faa 
    #grep 1703496 /data/databases/fasta/lizih/metagenemark_predictions.faa
done < "$file"

As a check I enter a grep line with specific number which is in the list and it works great! so seems like it has some problem with the '$f1'. When I'm doing echo "$f1", it printed the right numbers with no problems.

What might be the problem? Might it be related to 'string' type instead of number?

  • 2
    change '$f1' by "$f1" in grep-line – Costas Nov 4 '14 at 12:40
  • 1
    Please also give us an example of the ContigsNames file as well as the fasta file you are parsing. – terdon Nov 4 '14 at 13:33
2

You put single quotes around $f1 which causes that not to be expanded, you should use double quotes. Depending on the value of f1 it could be interpret as an option as Stephane indicated (instead of a regular expression), so you have to be explicit about this:

file=ContigsNames
while IFS=' ' read -r f1 f2
do
    grep -Fe "$f1" /data/databases/fasta/lizih/metagenemark_predictions.faa 
    #grep 1703496 /data/databases/fasta/lizih/metagenemark_predictions.faa
done < "$file"
  • Thanks! but even this:grep "$f1" /data/databases/fasta/lizih/metagenemark_predictions.faa doesn't work – lizi Nov 4 '14 at 12:52
  • 1
    @lizi And what is the error? – Anthon Nov 4 '14 at 12:57
  • $f1 is taken either as a regular expression or an option depending on whether it starts with - or not. Use grep -Fe "$f1" /data/databases/fasta/lizih/metagenemark_predictions.faa for it to be taken as a fixed string to look for in each line of the file. – Stéphane Chazelas Nov 4 '14 at 13:10
  • @lizi I updated the answer according to Stephane's comment. The problem might be in the content of the ContigsNames file. Can you retry and provide any errors if it still doesn't work? – Anthon Nov 4 '14 at 13:30
  • Thanks to all of you! I still have the problem. I don't have any error it just doesn't bring back any results (except printing the "$f1" as i asked in 'echo'). Example for the contigName is just number like 1703496. I had the -Fe, but still it doesn't work. – lizi Nov 5 '14 at 8:11

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.