我需要在模式匹配时删除行

我需要在模式匹配时删除行

我在 Linux 系统上有一个文件,aa.text其中包含以下行:

$ cat aa.text
I am a user
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [PCRMR2S]
Fri Feb 09 23:57:51 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:51 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:51 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:51 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:52 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:54 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:54 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:55 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:56 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:56 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:56 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:56 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:58:03 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
username is valid
Fri Feb 09 23:58:04 2024

我想得到如下输出:

i am a user
username is valid 
Fri Feb 09 23:58:04 2024

我试过:

sed -e '/ORA-1653/,+1 d' aa

这给了我:

i am a user
Fri Feb 09 23:57:51 2024
Fri Feb 09 23:57:51 2024
Fri Feb 09 23:57:51 2024
Fri Feb 09 23:57:52 2024
Fri Feb 09 23:57:54 2024
Fri Feb 09 23:57:55 2024
Fri Feb 09 23:57:56 2024
username is valid
Fri Feb 09 23:58:04 2024

但是我需要

i am a user
username is valid 
Fri Feb 09 23:58:04 2024

我怎样才能做到这一点?

要求不仅是在最后(行)打印时间戳。要求是如果找到匹配词(例如ORA-1653)并且随后的下一行是时间戳,则删除这两行,但如果未找到匹配词并且随后的下一行是时间戳,则不应删除该时间戳。

答案1

这会根据您提供的输入产生您期望的输出:

$ head -n 1 file; tail -n 2 file
I am a user
username is valid
Fri Feb 09 23:58:04 2024

如果这不是您所需要的全部,请编辑您的问题,告诉我们您选择要输出的 3 行的要求。

答案2

您可以使用-v密钥grep。如果没有它,grep将打印具有该模式的行,使用-vgrep将打印没有该模式的行。

由于您有两种类型的行需要忽略,因此您需要两种模式进行grep搜索。用 标记它们-e

所以你可以尝试这个:

grep -v -e "ORA-1653" -e "Fri Feb" aa.text

答案3

假设:

  • 有 3 种不同的线路格式...
  • 格式#1: 行开头为ORA
  • 格式#2:行包含格式的日期/时间戳Day Mon dd hh:mm:ss yyyy
  • 格式#3: 任何不匹配的内容格式#1或者格式#2
  • 目标是...
  • 忽略带有格式#1
  • 打印最后一行格式#2
  • 打印所有 ines格式#3

一个awk想法:

awk '
BEGIN    { regex = "[0-2][0-9]:[0-5][0-9]:[0-5][0-9]" }    # hh:mm:ss

/^ORA/   {                next }                           # format #1

NF==5 &&
$4~regex { last_dt = $0 ; next }                           # format #2

1                                                          # format #3 (ie, everything else)

END      { print last_dt }
' aa.txt

这会生成:

I am a user
username is valid
Fri Feb 09 23:58:04 2024

答案4

使用(以前称为 Perl_6)

~$ raku -e 'my @day-of-week = <Sun Mon Tue Wed Thu Fri Sat>;  \
            for slurp() { 
                put S:g/ ^^ ORA \- 1653 \: \h .+? $$ [\n ^^ @day-of-week .+? $$]? \n // 
            };  file

#或者

~$ raku -e 'my @day-of-week = <Sun Mon Tue Wed Thu Fri Sat>;  \
            S:g/^^ ORA \- 1653 \: \h .+? $$ [\n ^^ @day-of-week .+? $$]? \n //.put for slurp;'  file

上面是用 Raku(Perl 编程语言家族的成员)编写的答案。该文件被slurp编辑(一次读入内存),并且两个目标行在 Raku 的S///“big-S”替换运算符的识别域中描述(使用命名参数全局化S:g///)。第二行[\n ^^ Fri .+? $$]?对于枚举器来说是可选的?。替换的一半什么也没有,删除了识别的文本序列。

输入示例:

ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [PCRMR2S]
I am a user
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [PCRMR2S]
Fri Feb 09 23:57:51 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:51 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:51 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:51 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:52 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:54 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:54 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:55 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:56 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:56 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:56 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:57:56 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
Fri Feb 09 23:58:03 2024
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
ORA-1653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM [ABCD]
username is valid
Fri Feb 09 23:58:04 2024

示例输出:

I am a user
username is valid
Fri Feb 09 23:58:04 2024

\n上面的“示例输出”末尾似乎插入了一个换行符。如果这是一个问题,那么可以通过在和 最终.trim-trailing之间插入对例程的调用来解决它。S:g///.put


slurp如果您一次性拥有该文件的内存,则上述方法有效。如果大小是一个问题,下面的答案将逐行读取 input_file 。它使用 Raku 的三元运算符(与 Perl 的拼写略有不同):测试 ?? 真的 !! 错误的

~$ raku -e 'my @day-of-week = <Sun Mon Tue Wed Thu Fri Sat>; my $flag = 0; \
            for lines() { / ^ ORA \- 1653 \: \h .+ $ /          \
                ?? ( $flag=1 andthen next )                     \
                !! ( $flag == 1 and / ^ @day-of-week .+ $ /)    \
                    ?? ( $flag=0 andthen next )                 \
                    !! ( .put andthen $flag=0 ) };'   file
I am a user
username is valid
Fri Feb 09 23:58:04 2024

https://docs.raku.org/routine/slurp
https://docs.raku.org/language/regexes#S///_non-breaking_substitution
https://docs.raku.org/language/operators#infix_??_!
https://raku.org

相关内容