regex 如何让sed在字符串中输出组的所有出现

lmyy7pcs  于 2023-08-08  发布在  其他
关注(0)|答案(3)|浏览(109)

所以,我有一个这样的字符串:

Execution error for request 78471. Reason: ESS-07033 Job logic indicated a system error occurred while executing an asynchronous java job for request 78471. Job error is: JBO-26067: Failed to add JDBC connection to the connection pool, connection pool is full..

字符串
然后,我想从中提取3个东西,并使用sed管道|来分隔它。但是,需要注意的是,捕获组\2\3是可选的,当它们没有出现在输入字符串中时,我需要从sed输出为空。因此,我声明为可选,但我永远不会得到\2\3
我现在的sed表达式:

sed -E  -n 's/.*request ([0-9]+).* (ESS-[0-9]+).* (JBO-[0-9]+).*/\1|\2|\3/p'


输出78471|ESS-07033|JBO-26067
使用可选组:

sed -E  -n 's/.*request ([0-9]+).* (ESS-[0-9]+)?.* (JBO-[0-9]+)?.*/\1|\2|\3/p'


它在相同的输入上给出78471||的输出:
所以我需要

echo 'Execution error for request 78471. Reason: ESS-07033 Job logic indicated a system error occurred while executing an asynchronous java job for request 78471. Job error is: JBO-26067:' | sed ...


生成所有3个值,并且

echo 'Execution error for request 78471. Reason: ESS-07033 blah blah' | sed ...


生成78471|ESS-07033|

echo 'Execution error for request 78471. Reason: null' | sed ...


生产:78471||
我不知道我是否错过了sed的一些东西,或者我的正则表达式。理想情况下,我甚至可以减少正则表达式

7uhlpewt

7uhlpewt1#

在Perl中,您不能捕获可选组。但是您可以使用包含捕获组的非捕获可选组(?:...)?

#!/bin/bash
strings=(
    'Execution error for request 78471. Reason: ESS-07033 Job logic indicated a system error occurred while executing an asynchronous java job for request 78471. Job error is: JBO-26067:'
    'Execution error for request 78471. Reason: ESS-07033 blah blah'
    'Execution error for request 78471. Reason: null'
)

for s in "${strings[@]}" ; do
    perl -lne '($req, $rea, $job) = /request\ ([0-9]+)
                                    (?:.*
                                        (ESS-[0-9]+)
                                    )?
                                    (?:.*
                                        (JBO-[0-9]+)
                                    )?
                                    /x;
               print "$req|$rea|$job"' <<< "$s"
done

字符串
我使用了/x修饰符,这使得正则表达式的格式更易读(它忽略了非引号的空格)。

knpiaxh1

knpiaxh12#

$ sed -E 's/.* request ([0-9]+).*Reason: *(ESS-[0-9]+)?.* (JBO-[0-9]+)?.*/\1|\2|\3/g' file
78471|ESS-07033|JBO-26067
78471|ESS-07033|
78471||

$ cat file
Execution error for request 78471. Reason: ESS-07033 Job logic indicated a system error occurred while executing an asynchronous java job for request 78471. Job error is: JBO-26067:
Execution error for request 78471. Reason: ESS-07033 blah blah
Execution error for request 78471. Reason: null

字符串

vjrehmav

vjrehmav3#

这可能是你想要的,使用任何awk:

$ cat tst.awk
BEGIN { OFS="|" }
{
    req = ( match($0,/request /) ? substr($0,RSTART+RLENGTH) + 0 : "" )
    ess = ( match($0,/ESS-[0-9]+/) ? substr($0,RSTART,RLENGTH) : "" )
    jbo = ( match($0,/JBO-[0-9]+/) ? substr($0,RSTART,RLENGTH) : "" )
    print req, ess, jbo
}

字符串

$ awk -f tst.awk file
78471|ESS-07033|JBO-26067
78471|ESS-07033|
78471||


并借用创建的示例输入@Choroba:

$ cat file
Execution error for request 78471. Reason: ESS-07033 Job logic indicated a system error occurred while executing an asynchronous java job for request 78471. Job error is: JBO-26067:
Execution error for request 78471. Reason: ESS-07033 blah blah
Execution error for request 78471. Reason: null

相关问题