假设您有以下输入文件
Some text. It may contain line
breaks.
Some other part of the text
Yet an other part of
the text
并且您想要迭代每个文本部分(由两个换行符分隔(\n\n
)), 以便
在第一次迭代中我只会得到:
Some text. It may contain line
breaks.
在第二次迭代中我会得到:
Some other part of the text
在最后一次迭代中我会得到:
Yet an other part of
the text
我尝试过这个,但它似乎不起作用,因为IFS
只支持一个角色?
cat $inputfile | while IFS=$'\n\n' read part; do
# do something with $part
done
这是解决方案anubhava在纯bash中:
#!/bin/bash
COUNT=1; echo -n "$COUNT: "
while read LINE
do
[ "$LINE" ] && echo "$LINE" || { (( ++COUNT )); echo -n "$COUNT: " ;}
done
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)