1

Given a file with 2 columns, a string and a filename, how can these be looped with a grep command? Would also like to direct the output to same name as SEQFILE in different folder. Believe the following is close, with correct quoting missing:

while IFS=" ", read SEQID SEQFILE
do
grep -A1 -m 1 ${SEQID} ${SEQFILE} > ./tmp/${SEQFILE}
done < missingSeq.txt
4
  • 1
    Is the file comma or space delimited? Your IFS above would make me think either/both. Commented Jul 24, 2014 at 19:12
  • one thing is your ifs .. the second is your redirection operator > Commented Jul 24, 2014 at 19:14
  • what exactly you want to do with grep command Commented Jul 24, 2014 at 19:16
  • Can you be sure that the second field is unique over the missingSeq.txt file? Because if it is not, later occurrences will overwrite earlier. Commented Jul 24, 2014 at 19:20

2 Answers 2

2

If the file is comma delimited, then IFS=" ", makes no sense, but if it's whitespace delimited, then the comma is getting in the way. This is likely to solve the problem you present above:

while IFS=' ' read id file; do
    grep -A1 -m1 "$id" "$file" > "./tmp/$file"
done < missingSeq.txt
Sign up to request clarification or add additional context in comments.

1 Comment

You're right it is whitespace, and the extra comma was the error. The two column file is an error report from a job run in parallel, and it is needed to append each SEQID (and following line) for each filename. Thanks!
0
IFS=' '
while read seq_id seq_file; do
   grep -A1 -m1 "$seq_id" "$seq_file" >> ./tep/seq_file    # i assume "tep" was actually meant
done < missingSeq.txt

2 Comments

do the little do it clean
>> will append to the file..while > will overwrite

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.