开发者

Parsing a text stream with one word in each line in BASH

开发者 https://www.devze.com 2023-03-26 07:47 出处:网络
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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号