1

So I want to use grep with variables I'd be picking from a text file (one per line). I've tried different commands for grep and it's not working... I've seen also similar questions, but anything seems to be working? Any help would be much appreciated, thanks. Below my attempt, manually assigning the variable hey grep works, but not if I attempt to take the variable from the ids.txt file

Thank you very much!

$ head ids.txt 
Nitab4.5_0005105
Nitab4.5_0007510
Nitab4.5_0001604
$ for i in $(cat ids.txt); do   grep -A1 "$i" seqs.fasta; done
$ hey="Nitab4.5_0001604"
$ grep -A1 "$hey" seqs.fasta
>Nitab4.5_0001604
ATGTCATGCATTTTTTAAAANNNNNNNNNNNA
terdon
  • 242,166
JLR
  • 11
  • 1
  • 1
    Wrong line endings in the file? See this question. – Kamil Maciorowski Feb 28 '22 at 08:21
  • 2
    Please also add an example of the seqs.fasta file that you're grepping from. I also suggest to add echo to the beginng of your grep command in your loop, to ensure the command actually looks the way you think it does. And last thing, please run the following command: head ids.txt | cat -A to check if there are any imvisible escape characters as @KamilMaciorowski suggested. – aviro Feb 28 '22 at 08:33
  • 2
    grep can read patterns from a file: grep -f ids.txt seqs.fasta. Also note, that . is a special character for regex pattern. You might want to use -F to advice grep to use a fixed string instead. – pLumo Feb 28 '22 at 10:52
  • Thank you all for the help, indeed "^M" at the end of the lines. Working now. Thanks for your time. dos2unix fixed the issue – JLR Mar 01 '22 at 10:04

0 Answers0