根据模式“\r\n00”分割文件在科恩壳中

发布于 2025-01-16 00:14:32 字数 323 浏览 0 评论 0原文

我的文件 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 匹配确切的模式吗

My file temp.txt looks like below

00ABC
PQR123400
00XYZ001234
012345
0012233

I want to split the file based on pattern '\r\n00'. In this case temp.txt should split into 3 files

first.txt: 
00ABC
PQR123400

second.txt
00XYZ001234
012345

third.txt
0012233

I am trying to use csplit to match pattern '\r\n00' but the debug shows me invalid pattern. Can someone please help me to match the exact pattern using csplit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶…霜飞 2025-01-23 00:14:32

对于显示的示例,请尝试以下 awk 代码。用 GNU awk 编写和测试。

此代码将在您的系统中创建名称如下的文件:1.txt2.txt 等。这还将负责关闭后端的输出文件,这样我们就不会出现臭名昭著的错误打开的文件太多

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:""),$0) > 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:""),$0) > 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. 

With your shown samples, please try following awk code. Written and tested in GNU awk.

This code will create files with names like: 1.txt, 2.txt and so on in your system. This will also take care of closing output files in backend so that we don't get in-famous error too many files opened one.

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:""),$0) > outputFile
  close(outputFile)
  prevRT=rt
}
'  Input_file

Explanation: Adding detailed explanation for above code.

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:""),$0) > 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. 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文