假设您有以下输入文件
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
这是纯净bash中anubhava的解决方案:
#!/bin/bash
COUNT=1; echo -n "$COUNT: "
while read LINE
do
[ "$LINE" ] && echo "$LINE" || { (( ++COUNT )); echo -n "$COUNT: " ;}
done
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句