在终端中根据括号或圆括号(仅限顶级)分割文本文件

2024-03-19

我有几个文本文件 (utf-8),我想在 shell 脚本中处理它们。它们的格式并不完全相同,但如果我只能将它们分解成可食用的块,我就可以处理它。 这可以用 C 或 python 编程,但我不喜欢。

编辑:我用 C 编写了一个解决方案;看我自己的答案。我认为这可能是最简单的方法。如果您认为我错了,请根据我下面的答案中更复杂的示例输入来测试您的解决方案。

——jcxz100

为了清楚起见(并且能够更轻松地调试),我希望将块保存为子文件夹中的单独文本文件。

所有类型的输入文件都包含:

  1. 垃圾线路
  2. 包含垃圾文本的行,后跟开始方括号或圆括号 - 即 '[' '{' '
  3. 有效载荷线
  4. 带有嵌套在顶级对中的方括号或圆括号的行;也被视为有效负载
  5. 带有结束括号或括号的有效负载行 - 即 ']' '}' '>' 或 ')' - 后面可能跟着一些东西(垃圾文本和/或新有效负载的开始)

我想仅根据匹配对来分解输入顶层方括号/圆括号。 这些对内的有效负载不得更改(包括换行符和空格)。 顶级对之外的所有内容都应作为垃圾丢弃。

必须考虑双引号内的任何垃圾或有效负载atomic(作为原始文本处理,因此内部的任何括号或圆括号也应被视为文本)。

下面是一个示例(仅使用 {} 对):

junk text
"atomic junk"

some junk text followed by a start bracket { here is the actual payload
   more payload
   "atomic payload"
   nested start bracket { - all of this line is untouchable payload too
      here is more payload
      "yet more atomic payload; this one's got a smiley ;-)"
   end of nested bracket pair } - all of this line is untouchable payload too
   this is payload too
} trailing junk
intermittent junk
{
   payload that goes in second output file    }
end junk

...抱歉:有些输入文件确实如此混乱。

第一个输出文件应该是:

{ here is the actual payload
   more payload
   "atomic payload"
   nested start bracket { - all of this line is untouchable payload too
      here is more payload
      "yet more atomic payload; this one's got a smiley ;-)"
   end of nested bracket pair } - all of this line is untouchable payload too
   this is payload too
}

...和第二个输出文件:

{
   payload that goes in second output file    }

Note:

  • 我还没有完全决定是否有必要keep输出中的一对开始/结束字符,或者它们本身是否应作为垃圾丢弃。 我认为保留它们的解决方案是更通用的。

  • 同一输入文件中可以混合使用多种类型的顶级括号/圆括号对。

  • 注意:输入文件中有 * 和 $ 字符,因此请避免混淆 bash ;-)

  • 我更喜欢可读性而不是简洁性;但速度不会呈指数级下降。

必备品:

  • 文本内有反斜杠转义的双引号;最好应该处理它们 (我有一个 hack,但它并不漂亮)。

  • 该脚本不应该突破垃圾和/或有效负载中不匹配的方括号/圆括号对(注意:在原子内部,它们must被)允许!)

更远的好东西:

  • 我还没有看到它,但可以推测某些输入可能用单引号而不是双引号来表示原子内容......或者甚至是两者的混合。

  • 如果可以轻松修改脚本以解析类似结构但具有不同开始/结束字符或字符串的输入,那就太好了。

我可以看到这很拗口,但我认为如果我将其分解为更简单的问题,它不会给出一个可靠的解决方案。

主要问题是正确分割输入 - 其他所有内容都可以被忽略或通过黑客“解决”,所以 随意忽略锦上添花更遥远的美好事物.


Given:

$ cat file
junk text
"atomic junk"

some junk text followed by a start bracket { here is the actual payload
   more payload
   "atomic payload"
   nested start bracket { - all of this line is untouchable payload too
      here is more payload
      "yet more atomic payload; this one's got a smiley ;-)"
   end of nested bracket pair } - all of this line is untouchable payload too
   this is payload too
} trailing junk
intermittent junk
{
   payload that goes in second output file    }
end junk

这个 perl 文件会将您描述的块提取到文件中block_1, block_2, etc:

#!/usr/bin/perl
use v5.10;
use warnings;
use strict;

use Text::Balanced qw(extract_multiple extract_bracketed);

my $txt;

while (<>){$txt.=$_;}  # slurp the file

my @blocks = extract_multiple(
    $txt,
    [
        # Extract {...}
        sub { extract_bracketed($_[0], '{}') },
    ],
    # Return all the fields
    undef,
    # Throw out anything which does not match
    1
);
chdir "/tmp";
my $base="block_";
my $cnt=1;
for my $block (@blocks){ my $fn="$base$cnt";
                         say "writing $fn";
                         open (my $fh, '>', $fn) or die "Could not open file '$fn' $!";
                         print $fh "$block\n";
                         close $fh;
                         $cnt++;}

现在的文件:

$ cat block_1
{ here is the actual payload
   more payload
   "atomic payload"
   nested start bracket { - all of this line is untouchable payload too
      here is more payload
      "yet more atomic payload; this one's got a smiley ;-)"
   end of nested bracket pair } - all of this line is untouchable payload too
   this is payload too
}

$ cat block_2
{
   payload that goes in second output file    }

Using Text::Balanced是强大的并且可能是最好的解决方案。

You can用一个 Perl 做这些块regex https://regex101.com/r/3DtI1N/1:

$ perl -0777 -nlE 'while (/(\{(?:(?1)|[^{}]*+)++\})|[^{}\s]++/g) {if ($1) {$cnt++; say "block $cnt:== start:\n$1\n== end";}}' file
block 1:== start:
{ here is the actual payload
   more payload
   "atomic payload"
   nested start bracket { - all of this line is untouchable payload too
      here is more payload
      "yet more atomic payload; this one's got a smiley ;-)"
   end of nested bracket pair } - all of this line is untouchable payload too
   this is payload too
}
== end
block 2:== start:
{
   payload that goes in second output file    }
== end

但这比使用像这样的适当的解析器要脆弱一些Text::Balanced...

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在终端中根据括号或圆括号(仅限顶级)分割文本文件 的相关文章

  • 在Python中通过sys.stdout写入unicode字符串

    暂时假设一个人无法使用print 从而享受自动编码检测的好处 所以这给我们留下了sys stdout 然而 sys stdout太蠢了不做任何合理的编码 http bugs python org issue4947 现在人们阅读 Pytho
  • 从 Ruby 运行 shell 命令:在显示输出时捕获输出?

    我有个问题 我想从另一个 ruby 脚本运行一个 ruby 脚本并捕获它的输出信息 同时让它也输出到屏幕上 runner usr bin env ruby print Enter your password password gets ch
  • CentOS目录结构是树形的吗?

    CentOS 上有相当于树的东西吗 如果你的 Centos 系统上没有安装 tree 无论如何我通常建议服务器设置使用最小安装磁盘 你应该在命令行中输入以下内容 yum install tree y 如果没有安装 那是因为您没有正确的存储库
  • Bash - 在 perl 正则表达式中使用变量以及匹配组

    这是我在 stackoverflow 上的第一篇文章 如果我错过了一些重要的内容 请原谅我 我目前遇到以下问题 目标是根据我准备的文件列表动态替换端口号find 这些文件中的所有端口均以数字 4 开头 有 5 位数字 现在是棘手的部分 我只
  • Mac OS X 中 Bash 脚本中的 SFTP 命令

    我需要使用 SFTP 和 SSH 从 Mac 主机将文本文件传输到远程 PC freeSSH 这两个连接在本地网络中 那么有没有办法从 Bash 脚本内部运行 SFTP 命令 使用提供的用户名和密码 我已经尝试过一些脚本expect 但我没
  • 将 JSON 数组转换为 bash 数组并保留空格

    我想将 JSON 文件转换为 bash 字符串数组 稍后我可以对其进行迭代 我的JSON结构如下 USERID TMCCP CREATED DATE 31 01 2020 17 52 USERID TMCCP CREATED DATE 31
  • 如何将长选项与 Bash getopts 内置一起使用?

    我正在尝试解析 tempBash getopts 的选项 我这样调用我的脚本 myscript temp foo bar someFile 这是我用来解析选项的代码 while getopts temp shots o option do
  • 伊迪德信息

    重新定义问题 有什么方法可以获取所连接显示器的序列号吗 我想收集显示器的Eid信息 当我使用 logverbose 选项运行 X 时 我可以从 xorg 0 log 文件中获取它 但问题是 如果我切换显示器 拔出当前显示器 然后插入另一个显
  • 如何在 Xamarin.Mac 中执行终端命令并读入其输出

    我们正在编写一个 Xamarin Mac 应用程序 我们需要执行像 uptime 这样的命令 并将其输出读取到应用程序中进行解析 这可以做到吗 在 Swift 和 Objective C 中都有 NTask 但我似乎无法在 C 中找到任何示
  • 如何使 case 语句匹配数字范围?

    我正在运行一个列号范围为 0 50 的开关案例 现在每个案例都支持离散列号 我观察到它的失败 这是代码 i 10 a 1 b 0 65 if a b then case i in 1 2 5 echo Not OK 9 10 12 echo
  • 反转java String.split()效果的方法? [复制]

    这个问题在这里已经有答案了 我正在寻找一种将字符串数组组合成分隔符的方法 细绳 与 split 相反 在我尝试自己编写之前想询问一下论坛 因为 JDK 拥有一切 据我所知 JDK 中没有任何方法可以实现这一点 阿帕奇公共语言 http co
  • 文本处理问题:删除其中一列不包含特定值的行

    我有一个制表符分隔的文件 如下所示 input sequence match sequence score receptor group epitope antigen organism ASRPPGGVNEQF ASRPPGGVNEQF
  • 在 Fish Shell 中设置导出

    我安装了多个版本的 PHP 对于我的正常开发 我总是使用通过自制程序安装的 PHP 5 5 x 在鱼壳里 which php php version gt usr local bin php gt PHP 5 5 8 cli built J
  • bash 变量中的 Linux 鞭尾/对话框参数错误

    有人可以解释为什么下面的代码不起作用吗 我要疯狂地想找出答案 bin bash TEST M1 1 wire Interface ON echo TEST RESULT dialog title Config Modules State c
  • 如何在 Mac 上设置默认 shell,例如钓鱼? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我不喜欢重新输入fish每次我启动终端时 我想Fish https en wikipedia org wiki Fish Unix she
  • 给出 5 个参数,但在终端中只得到 3 个参数

    我想将一个文件传递给一个c 程序 如果我在 IDE 中执行此操作 test string string lt test txt return argc 5 但在终端上我刚刚得到argc 3 看来 这是因为 什么是 lt 意思是 我正在使用
  • shell_exec 的输出被截断为 100 个字符

    当在 shell 中运行以下命令时 curl F file filename http 192 168 0 1 产生以下输出 Accuracy 0 0 1 classification Accuracy 0 0 1 classificati
  • 使用带有curl 的内部字段分隔符

    当我做 ls IFS l 我得到了我期望的输出 当我做 curl IFShttp www google com 我不 我是否误解了内部字段分隔符 如何在不使用任何空格字符的情况下运行curl 命令 您需要将变量放在大括号内 否则 shell
  • awk 子串单个字符

    这是columns txt aaa bbb 3 ccc ddd 2 eee fff 1 3 3 g 3 hhh i jjj 3 kkk ll 3 mm nn oo 3 我可以找到第二列以 b 开头的行 awk if substr 2 1 1
  • Bash 解析和 shell 扩展

    我对 bash 解析输入和执行扩展的方式感到困惑 对于输入来说 hello world 作为 bash 中的参数传递给显示其输入内容的脚本 我不太确定 Bash 如何解析它 Example var hello world displaywh

随机推荐