使用终端将文本拆分为常规重复文本字符串之间的文件

问题描述 投票:0回答:1

我在定期间隔之间有几个文本块,我试图使用 shell 将它们解析为单独的文件。 该文件类似于以下格式:

<Line delimiter>
The lines of text to extract
<Line delimiter>
Other lines that are to be extracted but
also have an unpredictable length
<Line delimiter>
...
...
<Line delimiter>
and so forth

我正在尝试使用

Split
命令并在行之间进行 grep 操作:

for line in $(grep -n 'Line Delimiter' target.txt | cut -d ':' -f 1); do split -l $line target.txt  ; done 

同也,这个有头|尾管似乎是这个示例中看到的雄辩的解决方案之一。 我已经进行了更多搜索,并进行了一些其他尝试,但我觉得这应该相对容易,但事实证明,在超过 1 次迭代中完成这一壮举是很困难的。 如有任何帮助,请提前致谢。

shell terminal zsh
1个回答
0
投票

简单的例子希望有帮助

$ cat input.txt
<Line delimiter>
The lines of text to extract
<Line delimiter>
Other lines that are to be extracted but
also have an unpredictable length
<Line delimiter>
these should also go...
into another file ...
<Line delimiter>
and so forth
$
$ #repeat the split as many times as the match is made
$ csplit -q input.txt '/<Line delimiter>/' '{*}'
$
$ # file xx00 is empty as the first occurence of <Line delimiter> is line 1   
$ cat xx01
<Line delimiter>
The lines of text to extract
$ cat xx02
<Line delimiter>
Other lines that are to be extracted but
also have an unpredictable length
$ cat xx03
<Line delimiter>
these should also go...
into another file ...
$ cat xx04
<Line delimiter>
and so forth

查看 csplit 联机帮助页以获取更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.