Perlnika
Perlnika

Reputation: 5066

bash - variable storing multiple lines of file

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.

Upvotes: 0

Views: 841

Answers (3)

potong
potong

Reputation: 58420

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

Upvotes: 0

lynxlynxlynx
lynxlynxlynx

Reputation: 1433

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.

Upvotes: 3

Lynch
Lynch

Reputation: 9474

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;
)

Upvotes: 2

Related Questions