根据 korn shell 中的模式 '\r\n00' 拆分文件

Splitting file based on pattern '\r\n00' in korn shell

我的文件 temp.txt 如下所示

00ABC
PQR123400
00XYZ001234
012345
0012233

我想根据模式“\r\n00”拆分文件。在这种情况下 temp.txt 应该分成 3 个文件

first.txt: 
00ABC
PQR123400

second.txt
00XYZ001234
012345

third.txt
0012233

我正在尝试使用 csplit 来匹配模式“\r\n00”,但调试显示无效模式。有人可以帮我使用 csplit

来匹配确切的模式吗

使用您显示的示例,请尝试以下 awk 代码。在 GNU awk.

中编写和测试

此代码将在您的系统中创建名称类似于:1.txt2.txt 等的文件。这也将负责关闭后端的输出文件,这样我们就不会得到 in-famous 错误 too many files opened 一个。

awk -v RS='\r?\n00' -v count="1" '
{
  outputFile=(count++".txt")
  rt=RT
  sub(/\r?\n/,"",rt)
  if(!rt){
    sub(/\n+/,"")
    rt=prevRT
  }
  printf("%s%s\n",(count>2?rt:""),[=10=]) > outputFile
  close(outputFile)
  prevRT=rt
}
'  Input_file

说明:为以上代码添加详细说明。

awk -v RS='\r?\n00' -v count="1" '      ##Starting awk program from here and setting RS as \r?\n00 aong with that setting count as 1 here.
{
  outputFile=(count++".txt")            ##Creating outputFile which has value of count(increases each time cursor comes here) followed by .txt here.
  rt=RT                                 ##Setting RT value to rt here.
  sub(/\r?\n/,"",rt)                    ##Substituting \r?\n with NULL in rt.
  if(!rt){                              ##If rt is NULL then do following.
    sub(/\n+/,"")                       ##Substituting new lines 1 or more with NULL.
    rt=prevRT                           ##Setting preRT to rt here.
  }
  printf("%s%s\n",(count>2?rt:""),[=11=]) > outputFile   ##Printing rt and current line into outputFile.
  close(outputFile)                     ##Closing outputFile in backend.
  prevRT=rt                             ##Setting rt to prevRT here.
}
'  Input_file                           ##Mentioning Input_file name here.