gpt4 book ai didi

linux - 比较字符串并将值连接到 bash shell 中的一个字段中

转载 作者:太空宇宙 更新时间:2023-11-04 05:03:14 24 4
gpt4 key购买 nike


我正在编写一个脚本来修改 csv 文件

这是我的 csv 文件:

"ID", "Subject" , "Channels", "Description"
"24" , "Bind-0.9.3" , "Linux", "BIND (Berkeley Internet Name Domain) is an implementation of the DNS (Domain Name System) protocols"
"24" , "Bind-0.9.3", "Fedora", "BIND (Berkeley Internet Name Domain) is an implementation of the DNS (Domain Name System) protocols"
"25" , "Tar-8.0.1" , "Debian", "Tar Package"
"25" , "Tar-8.0.1", "Ubuntu" , "Tar Package"

现在,我想比较“ID”值。如果它们的值相同,我们可以将“Channels”加入一个字段

这是预期结果:

"ID", "Subject" , "Channels", "Description"
"24" , "Bind-0.9.3" , "Linux , Fedora", "BIND (Berkeley Internet Name Domain) is an implementation of the DNS (Domain Name System) protocols"
"25" , "Tar-8.0.1" , "Debian , Ubuntu", "Tar Package"

有人知道在我的例子中使用 awk、sed 或其他东西吗?
非常感谢
问候,

最佳答案

$ cat tst.awk
BEGIN { FS="[[:space:]]*,[[:space:]]*"; OFS=" , " }
NR==1 { print; next }
{
subj[$1] = $2
desc[$1] = $4
if ($1 in chans) {
chans[$1] = chans[$1] OFS $3
}
else {
chans[$1] = $3
cnt2chan[++numChans] = $1
}
}
END {
for (chanNr=1; chanNr<=numChans; chanNr++) {
chan = cnt2chan[chanNr]
gsub(/\"/,"",chans[chan])
print chan, subj[chan], "\"" chans[chan] "\"", desc[chan]
}
}
$
$ awk -f tst.awk file
"ID", "Subject" , "Channels", "Description"
"24" , "Bind-0.9.3" , "Linux , Fedora" , "BIND (Berkeley Internet Name Domain) is an implementation of the DNS (Domain Name System) protocols"
"25" , "Tar-8.0.1" , "Debian , Ubuntu" , "Tar Package"

关于linux - 比较字符串并将值连接到 bash shell 中的一个字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26332027/

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