0

If have the following file /tmp/f1.txt file coming from the stdout redirection of the result of a PostgreSQL bash script execution :

 id  
-----
 517
 518
 519
 520
 521
 522
(6 rows)

INSERT 0 6

I wish I could iterate over those ids to perform other actions.

For now, I am able to retrieve the desired ids as follows (i'll always get 5 values ) :

cat /tmp/f1.txt | tail -n +3 | head -n5 -

This returns :

517
518
519
520
521

Which is exactly what I want.

So, the next step would be to read every single line. So in my case I'd store the result in a variable, and then read line by line :

LINES_UPDATED_CNT=$(cat /tmp/f1.txt | tail -n +3 | head -n${LINES} -)
echo -e $LINES_UPDATED_CNT

My issue here is that this command yields the following result :

517 518 519 520 521 522

The newlines are lost, can't understand why. What would be the optimal way to do this, without wirting the cat/tail/head result into another file and reading it in a while ?

Thank you !

SCO
  • 152
  • Looked at the link, and found out that eval function is to be used. The link looks like to hold many interesting and useful information, although understanding is not easy. I was able to finally get into a solution. Shall I provide the final solution or do you prefer to submit it afshin ? – SCO Dec 14 '17 at 11:21
  • there's no reason to eval, it's the unquoted expansion in the echo that makes your newlines disappear. But you don't need the variable either, just direct the first pipeline to a while read via pipe or process substitution – ilkkachu Dec 14 '17 at 12:05

0 Answers0