gpt4 book ai didi

bash - 在文本文件中根据另一个文件用其他模式替换模式

转载 作者:行者123 更新时间:2023-11-29 09:49:39 25 4
gpt4 key购买 nike

例子1

假设我在 file1.txt 中有这个:

line 1
line 45
line 3
line 2
line 24
line 1

file2.txt 中改为:

line 1,WWWW
line 2,EEE
line 3,RRR

我想要的是查看 file2.txt,搜索 , 之前的所有术语,并用 ,< 之后的术语替换它们file1.txt 中。我希望忽略 file2.txt 中不存在的所有行,保留顺序。

所以,预期的输出应该是,file1.txt:

WWW
line 45
RRR
EEE
line 24
WWW

例子2

现在,另一个满足不同需求的例子:file1.txt:

line1 1
line22 78
line32 65
line3 3
line2 2
line2 2

file2.txt:

line1 1,SONG1 playing: X | NAME1
line2 2,SONG2 playing: Y | NAME2
line3 3,SONG3 playing: Z | NAME3

预期输出应该是:

SONG1 playing: X | NAME1
line22 78
line32 65
SONG3 playing: Z | NAME3
SONG2 playing: Y | NAME2
SONG2 playing: Y | NAME2

请记住,该脚本包含成百上千行(超过 5 MB 的文本)。

最佳答案

EDIT2:因为 OP 添加了更多有问题的场景来添加此代码以涵盖现在。

awk 'FNR==NR{val=$1;$1="";sub(/^ +/,"");a[val]=$0;next}  $0 in a{$0=a[$0]} 1' FS=',' file2.txt FS=' ' file1.txt

输出如下。

SONG1 playing: X | NAME1
line22 78
line32 65
SONG3 playing: Z | NAME3
SONG2 playing: Y | NAME2
SONG2 playing: Y | NAME2

enter image description here



编辑:由于 OP 更改了 Input_file 的示例和预期输出,因此现在添加此解决方案。

awk '
FNR==NR{
a[$1 OFS $2]=$NF
next
}
a[$0]{
$0=a[$0]
}
1
' FS='[, ]' file2.txt FS=" " file1.txt

运行时会输出如下代码:

awk '
FNR==NR{
a[$1 OFS $2]=$NF
next
}
a[$0]{
$0=a[$0]
}
1
' FS='[, ]' file2.txt FS=" " file1.txt
WWWW
line 45
RRR
EEE
line 24
WWWW


能否请您尝试以下。

awk '
FNR==NR{
a[$1 OFS $2]=$NF
next
}
a[$0]{
print a[$0]
}
' FS='[, ]' file2.txt FS=" " file1.txt > temp && mv temp file1.txt

说明:为上述代码添加说明。

awk '                      ##Starting awk program from here.
FNR==NR{ ##Checking condition FNR==NR which will be TRUE when file2.txt is being read.
a[$1 OFS $2]=$NF ##Creating an array named a whose index is $1 OFS $2 and value is $NF.
next ##next will skip further statements from here.
}
a[$0]{ ##Checking condition if a[$0] array a whose index $0 is NOT NULL then do following.
print a[$0] ##Printing value of array a with index $0.
}
' FS='[, ]' file2.txt FS=" " file1.txt > temp && mv temp file1.txt ##Setting FS as comma OR space for file2.txt AND setting FS as space for file1.txt

关于bash - 在文本文件中根据另一个文件用其他模式替换模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59036221/

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