gpt4 book ai didi

linux - 在 GNU/Linux 中,匹配文件中的模式并使用找到的字符串提取包含另一个文件中的字符串的行

转载 作者:太空宇宙 更新时间:2023-11-04 04:57:28 26 4
gpt4 key购买 nike

我是 shell 脚本世界的新手。

文件b.txt包含逗号分隔文本格式的错误代码。错误代码可以与此模式匹配 - '[A-Z]\{2\}-[A-Z0-9]\{4\}'。 b.txt 示例

LO-5645,SE-DH68,MY-2255,MI-9878,SY-FC25,
ER-55R8,LO-5645,
EU-1C07,ER-9871,EY-5523,MM-2564,
FO-D389,XU-2659,EU-1568,
etc etc....

文件a.txt的每一行包含一个错误代码和错误代码的描述。 a.txt 示例:

EU-1568: system not initializing
ER-55R8: fatal error on platform xx22
MM-2564: Driver not initialized
LO-24DE: Lot failed
SY-FC25: System error on domain
etc etc.....

我想合并这两个文件中的信息,以便创建一个文件 c.txt,其中包含逗号分隔的错误以及从 b.txt 中提取的错误消息的描述。

c.txt 中的预期结果示例

LO-5645,SE-DH68,MY-2255,MI-9878,SY-FC25: System error on domain,
ER-55R8: fatal error on platform xx22,LO-5645,
EU-1C07,ER-9871,EY-5523,MM-2564: Driver not initialized,
FO-D389,XU-2659,EU-1568: system not initializing,
etc etc...

我解决这个问题的想法:我尝试使用 while 循环逐行读取 b.txt,并使用 grep -o 将错误代码的模式精确匹配到数组变量中。然后使用内部 For 循环尝试一次读取该数组的一个元素,并匹配 a.txt 中包含错误代码的行。

如果我能得到您关于如何以更好的方式实现此解决方案的一些想法,那就太好了。

欢迎使用 awk、Sed、grep、perl、cut。

最佳答案

寻找完全匹配

awk -F'[,:]' -v OFS=',' '
FNR==NR{error[$1]=$NF;next}
{
for(i=1; i<=NF;i++)if($i in error)$i=$i":"error[$i]
}1' a.txt b.txt >c.txt

说明

awk -F'[,:]' -v OFS=',' '                 # Call awk, set input field sep
# , and : awk supports multiple field sep
# and output field sep as comma

# Here we read file a.txt

FNR==NR{ # this is true when awk reads first file
# When awk reads from the multiple input file,
# NR variable will give the total number
# of records relative to all the input file.
# FNR will give you number of records
# for each input file.

error[$1]=$NF; # populate array named error
# such that array index is col1
# and array value is last field of record
# NF gives no of fields in current record

next # The next statement forces awk to immediately
# stop processing the current record and
# go on to the next record

}

# Here we read file b.txt

{
# NF gives no fields in current record,
# start loop from first field/column to last field/column( NF )
# increment by 1

for(i=1; i<=NF;i++)

# check if column value exists in array error

if($i in error)

# if above if statement is true, then we
# have error description so
# modify current column
# current column = current column : and your description
# which exists in error array

$i=$i":"error[$i]

}1 # 1 at then does default operation print $0 (print current row/record)
' a.txt b.txt >c.txt

输入

$ cat a.txt 
EU-1568: system not initializing
ER-55R8: fatal error on platform xx22
MM-2564: Driver not initialized
LO-24DE: Lot failed
SY-FC25: System error on domain
etc etc.....

$ cat b.txt
LO-5645,SE-DH68,MY-2255,MI-9878,SY-FC25,
ER-55R8,LO-5645,
EU-1C07,ER-9871,EY-5523,MM-2564,
FO-D389,XU-2659,EU-1568,
etc etc....

输出

$ awk -F'[,:]' -v OFS=',' '
FNR==NR{error[$1]=$NF;next}
{
for(i=1; i<=NF;i++)if($i in error)$i=$i":"error[$i]
}1' a.txt b.txt
LO-5645,SE-DH68,MY-2255,MI-9878,SY-FC25: System error on domain,
ER-55R8: fatal error on platform xx22,LO-5645,
EU-1C07,ER-9871,EY-5523,MM-2564: Driver not initialized,
FO-D389,XU-2659,EU-1568: system not initializing,
etc etc....

关于linux - 在 GNU/Linux 中,匹配文件中的模式并使用找到的字符串提取包含另一个文件中的字符串的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42494316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com