4

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) 
1
  • It would be wise to set the interpreter explicitly, since you are running this in bash as #!/bin/bash or wherever it is installed in your machine Commented Jun 22, 2017 at 11:03

3 Answers 3

8

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

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

1 Comment

4

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 

Comments

3

You seem to want to pipe the output from one command to another. If so, use a pipe:

sed -n 1,1000p data/output.txt | while read -r line; do echo "$line"; done 

Or, using the right tool for the right job:

head -1000 data/output.txt | while read -r ; do something; done 

1 Comment

The issue with this approach is that the while construct is in a subprocess and does not affect variables outside of its scope, whereas input from a fifo or <(…) allows modifying outside variables.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.