0

I seem to have a little bit of an issue with spaces when trying to use for loops and grep together. The whitespace is important because, for example, I'd like to match 'k117_19650 ', but not 'k117_196509 '. Help would be appreciated!

for i in 'k117_19650 ' 'k117_460555 ';do
    grep -A1 $i final.contigs.fa >> gene.fa
done
Laura
  • 199

2 Answers2

3

The problem you are facing is related to your use of shell variables. In particular when using variables which contain "non-standard" characters such as whitespace, always quote your variables when using them, see e.g. discussions here:

In your case:

grep -A1 -- "$i" final.contigs.fa >> gene.fa

should do the trick (here also adding a -- to guard against values of $i that would start with -).

AdminBee
  • 22,803
2

For the purpose of such separation (match 'k117_19650 ', but not 'k117_196509 ' ) you can use -w in grep which will make it search for entire word instead of substring.

for i in k117_19650 k117_460555 ;do
    grep -w -A1 "$i" final.contigs.fa >> gene.fa
done
Romeo Ninov
  • 17,484