简体   繁体   English

分割每隔第二个分隔符

[英]Split every second occurrence of delimiter

I am trying to use awk to split a file every second occurrence of a delimiter, but I always end up with an empty file at the beginning and I can't understand why. 我试图使用awk每隔第二次出现一个分隔符来分割一个文件,但是我总是以开头的空文件结尾,我不明白为什么。

The data I need to break down in multiple files has a format similar to this: 我需要在多个文件中分解的数据具有类似于以下格式:

----------
aaa
bbb
----------
ccc
ddd
----------
eee
fff
----------
ggg  

The first resulting file should contain: 第一个生成的文件应包含:

----------
aaa
bbb
----------
ccc
ddd

The delimiter is always the same (10 times a 'minus' sign). 分隔符始终相同(“负号”的10倍)。
I am trying to do it like this for now: 我现在正在尝试这样做:

awk -v RS='[-]{10}' '{i++} {file = sprintf("temp-%s", int(i/2)); print >> file;}'

The first file I get however (temp-0) always includes an empty line and nothing else. 但是,我得到的第一个文件(temp-0)始终包含一个空行,没有其他内容。
Also, the source file does not start with an empty line, nor it has any in its content (they have been removed previously). 另外,源文件也不以空行开头,也没有内容(它们已在以前被删除)。

Can anybody please help? 有人可以帮忙吗?

I wouldn't play with RS for this problem. 对于这个问题,我不会使用RS You can count the --------- to decide if you have to increment the file index. 您可以数---------来决定是否必须增加文件索引。 Give this line a try: 试试这一行:

awk '/^--*$/{c++;f+=c%2?1:0}{print > "temp-"f}' file

Note that the above line gives you the idea of how to process the line and file index. 请注意,上面的行为您提供了如何处理行和文件索引的想法。 If your file is huge, you need close() the file and using >> to redirect again, otherwise you will get errors like too many opened files . 如果文件很大,则需要close()文件,然后使用>>再次重定向,否则会出现错误,例如too many opened files

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM