0

This is my code:

grep $to_check $forbidden >${dir}variants_of_interest;

cat ${dir}variants_of_interest | (while read line; do 
     #process ${line} and echo result
done;
)

Thank to grep I get lines of data that I then process separately in loop. I would like to use variable instead of using file variants_of_interest.

Reason for this is that I am afraid that writing to file thousands of time (and consequently reading from it) rapidly slows down computation, so I am hoping that avoiding writing to file could help. What do you think?

I have to do thousands of grep commands and variants_of_interest contains up to 10 lines only.

Thanks for your suggestions.

3 Answers 3

3

You can just make grep pass its output directly to the loop:

grep "$to_check" "$forbidden" | while read line; do 
  #process "${line}" and echo result
done

I removed the explicit subshell in your example, since it is already in a separate one due to the piping. Also don't forget to quote the $line variable to prevent whitespace expansion on use.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, this is working solution. Because you and Lynch were just few seconds apart, I chose the answer randomly.
Ideally you should pick the best answer as the final one — these two are not identical.
2

You dont have to write a file. Simply iterate over the result of grep:

grep $to_check $forbidden | (while read line; do 
     #process ${line} and echo result
done;
)

1 Comment

Thank you, this worked:) I ticked the answer randomly as you and lynxlynxlynx were few seconds apart.
0

This might work for you:

OIFS="$IFS"; IFS=$'\n'; lines=($(grep $to_check $forbidden)); IFS="$OIFS"
for line in "${lines[@]}"; do echo $(process ${line}); done

The first line places the results of the grep into the variable array lines.

The second line processes the array lines placing each line into the variable line

Comments

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.