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 !
eval
, it's the unquoted expansion in theecho
that makes your newlines disappear. But you don't need the variable either, just direct the first pipeline to awhile read
via pipe or process substitution – ilkkachu Dec 14 '17 at 12:05