Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash command to redirect stdin in while loop (using sed)

I am trying to stdin only lines from 1 to 1000 from a file (output.txt) to a while loop.

I have tried something like this:

#!/bin/bash
while read -r line; do
    echo "$line"
done < (sed -n 1,1000p data/output.txt)
like image 463
Jonathan Avatar asked Dec 23 '22 16:12

Jonathan


2 Answers

Just tried:

#!/bin/bash
while read -r line; do
    echo "$line"
done < <(sed -n 1,1000p data/output.txt)

adding another angular bracket "<" did the trick... If someone can explain that could be interesting.

Thanks

like image 74
Jonathan Avatar answered Jan 09 '23 14:01

Jonathan


the part <( ), is called process substitution, it can replace a filename in a command.

fifos can also be used to do the same thing.

mkfifo myfifo

sed -n 1,1000p data/output.txt > myfifo &

while read -r line; do
    echo "$line"
done < myfifo
like image 30
Nahuel Fouilleul Avatar answered Jan 09 '23 15:01

Nahuel Fouilleul