【问题标题】:Chunk text files with BASH使用 BASH 分块文本文件
【发布时间】:2018-07-19 18:42:25
【问题描述】:

我对 Bash 比较陌生,我想将一个包含在 zip 文件中的 txt 分块。块必须从“channel8”的​​ 5 行到“channel10”的 2 行

12082008;pull done;ret=34;Y
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
Channel8
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
Channel9
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
Channel10
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
Channel11
01062008;psuh done;ret=23;Y

到目前为止,我只成功分块到 10 频道,见下文:

12082008;pull done;ret=34;Y
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
Channel8
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
Channel9
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
Channel10
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N

但我不知道如何使用我的 bash 输出从通道 8 中进行选择。

这是我的代码

for file in ./*.zip; do head -$(($(unzip -c $file | grep -n $channel_after | cut -f1,1 -d":")-1)) <(unzip -c $file);done

【问题讨论】:

  • 发布预期的输出。不清楚当您说“直到通道 10”时,您的意思是“直到包含字符串 channel10 的行”或“直到以行 channel10 开头的块的末尾”或其他内容不同的人在他们的答案中做出不同的假设。

标签: bash grep zip tail unix-head


【解决方案1】:

sed 会做这项工作吗?

unzip -c $file | sed -n '/Channel8/,/Channel10/p'

sed 命令将打印Channel8Channel10 之间的所有内容,包括Channel 行。

如果您不想包含 Channel 行,您可以使用另一个 sed 来删除而不是打印。

unzip -c $file | sed '1,/Channel8/d;/Channel10/,$d'

【讨论】:

  • 谢谢!如果我想从 Channel8 分块 4 行,从 Channel10 分块 2 行怎么办?现在考虑单词 channel 不在行首。
【解决方案2】:

听起来你需要的是:

unzip -c "$file" |
    awk '
        /^Channel8$/ { seenBeg = 1 }

        seenBeg {
            if ( seenEnd && /^Channel/ ) {
                exit
            }
            else if ( /^Channel10$/ {
                seenEnd = 1
            }
            print
        }
    '

【讨论】:

    猜你喜欢
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 2023-04-08
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多