Parsing a text stream with one word in each line in BASH
I have a text stream which has n lines and each line is one word.
I would like to write something like a for loop that VAR
gets at every iteration the i'th line (from line 1 until EOF) and to run a com开发者_开发知识库mand on each.
Thanks!
In some cases the xargs
command can be sufficient:
my_stream_generator | xargs -n1 my_command
This assumes that the way of passing the words from the stream to the command is by specifying that word as an argument for the command.
If this is not the case @aioobe's suggestion might be more suitable for you.
while read VAR
do echo $VAR # just print the word on the line
done
Add < yourfile.txt
after done
if you need to read the words from some file.
ideone.com demo
精彩评论