分割文件由多个换行符 [英] Split file by multiple line breaks
本文介绍了分割文件由多个换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设你有以下输入文件
一些文字。它可能包含行
休息。文字的其它部分然而,在其他部分
文本
和要遍历每个文本部分,从而使(由两个换行符( \\ n \\ n
)隔开)
在第一次迭代我只会得到:
一些文字。它可能包含行
休息。
在第二次迭代我会得到:
文字的其它部分
而在最后一次迭代我会得到:
还有一个其它部分
文本
我想这一点,但它似乎并没有工作,因为 IFS
只支持一个字符?
猫$ inputfile中|而IFS = $'\\ n \\ n读一部分;做
#使用$部分的东西
DONE
解决方案
这是该解决方案的 anubhava 在纯庆典:
#!/斌/庆典COUNT = 1;回声-n$ COUNT
而读线
做
[$ LINE]放;&安培;回声$ LINE|| {((++ COUNT));回声-n$ COUNT:;}
DONE
Let's say you have the following input file
Some text. It may contain line
breaks.
Some other part of the text
Yet an other part of
the text
And you want to iterate each text part (seperated by two line breaks (\n\n
)), so that
in the first iteration I would only get:
Some text. It may contain line
breaks.
In the second iteration I would get:
Some other part of the text
And in the last iteration I would get:
Yet an other part of
the text
I tried this, but it doesn't seem to work because IFS
only supports one character?
cat $inputfile | while IFS=$'\n\n' read part; do
# do something with $part
done
解决方案
This is the solution of anubhava in pure bash:
#!/bin/bash
COUNT=1; echo -n "$COUNT: "
while read LINE
do
[ "$LINE" ] && echo "$LINE" || { (( ++COUNT )); echo -n "$COUNT: " ;}
done
这篇关于分割文件由多个换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文