当使用Shell脚本在第二个文件中查找中替换文件中时,为什么我会在某些行上获得空白?

发布于 2025-01-30 06:45:49 字数 1852 浏览 2 评论 0原文

我有一个文件data.txt,带有内容

2013-04-24;1;0.1635;1.4135;fp.3.Luci_02C06.ctg.ctg7180000085546
2013-04-24;1;0.9135;1.4135;fp.3.Luci_01A01.ctg.ctg7180000038386
2017-04-24;2;0.9135;1.4135;fp.3.Luci_02C06.ctg.ctg7180000085546
2011-04-24;2;0.9135;1.4135;fp.3.Luci_02C06.ctg.ctg7180000085546
2012-04-24;2;0.9135;1.4135;fp.3.Luci_02C06.ctg.ctg7180000085549
2016-04-24;2;0.9135;1.4135;fp.3.Luci_02C06.ctg.ctg7180000085549
2016-04-24;2;0.9135;1.4335;fp.3.Luci_02C06.ctg.ctg7180000085549
2013-04-24;1;0.9135;1.4135;fp.3.Luci_01A01.ctg.ctg7180000038386
2011-04-24;2;0.9135;1.4135;fp.3.Luci_02C06.ctg.ctg7180000085546

,另一个文件lookup.txt,内容

1;2012-04-24;2ab1e4c0-de4d-11e2-a934-0f0479162b1b;fp.3.Luci_02C06.ctg.ctg7180000085546
7;2013-04-24;2ab21e90-de4d-11e2-9ce8-d368d9512bad;fp.3.Luci_01A01.ctg.ctg7180000038386
3;2014-04-24;2ab2582e-de4d-11e2-bb5f-6b1f6c4437f8;fp.3.Luci_02C06.ctg.ctg7180000085549

我想在lookup.txt中的第4列中的匹配值中替换data.txt中的第5列.txt中的第5列。我想要的结果是

2013-04-24;1;0.1635;1.4135;1
2013-04-24;1;0.9135;1.4135;7
2017-04-24;2;0.9135;1.4135;1
2011-04-24;2;0.9135;1.4135;1
2012-04-24;2;0.9135;1.4135;3
2016-04-24;2;0.9135;1.4135;3
2016-04-24;2;0.9135;1.4335;3
2013-04-24;1;0.9135;1.4135;7
2011-04-24;2;0.9135;1.4135;1

i 另一篇文章我找到了以下看起来有希望的

awk -f -f';'; 'nr == fnr {a [$ 4] = $ 1; next} {$ 5 = a [$ 5]} 1'lookup.txt data.txt

,以下结果出于某种原因为第5列,第5列, 6,7和9。为什么?我们获得太空分离器而不是“”的事实;也是一个问题,但不那么重要

2013-04-24 1 0.1635 1.4135 1
2013-04-24 1 0.9135 1.4135 7
2017-04-24 2 0.9135 1.4135 1
2011-04-24 2 0.9135 1.4135 1
2012-04-24 2 0.9135 1.4135
2016-04-24 2 0.9135 1.4135
2016-04-24 2 0.9135 1.4335
2013-04-24 1 0.9135 1.4135 7
2011-04-24 2 0.9135 1.4135

I have one file data.txt with content

2013-04-24;1;0.1635;1.4135;fp.3.Luci_02C06.ctg.ctg7180000085546
2013-04-24;1;0.9135;1.4135;fp.3.Luci_01A01.ctg.ctg7180000038386
2017-04-24;2;0.9135;1.4135;fp.3.Luci_02C06.ctg.ctg7180000085546
2011-04-24;2;0.9135;1.4135;fp.3.Luci_02C06.ctg.ctg7180000085546
2012-04-24;2;0.9135;1.4135;fp.3.Luci_02C06.ctg.ctg7180000085549
2016-04-24;2;0.9135;1.4135;fp.3.Luci_02C06.ctg.ctg7180000085549
2016-04-24;2;0.9135;1.4335;fp.3.Luci_02C06.ctg.ctg7180000085549
2013-04-24;1;0.9135;1.4135;fp.3.Luci_01A01.ctg.ctg7180000038386
2011-04-24;2;0.9135;1.4135;fp.3.Luci_02C06.ctg.ctg7180000085546

and another file lookup.txt with content

1;2012-04-24;2ab1e4c0-de4d-11e2-a934-0f0479162b1b;fp.3.Luci_02C06.ctg.ctg7180000085546
7;2013-04-24;2ab21e90-de4d-11e2-9ce8-d368d9512bad;fp.3.Luci_01A01.ctg.ctg7180000038386
3;2014-04-24;2ab2582e-de4d-11e2-bb5f-6b1f6c4437f8;fp.3.Luci_02C06.ctg.ctg7180000085549

I want to replace column 5 in data.txt with column 1 in lookup.txt based on matching values in column 4 in lookup.txt. The result I want is

2013-04-24;1;0.1635;1.4135;1
2013-04-24;1;0.9135;1.4135;7
2017-04-24;2;0.9135;1.4135;1
2011-04-24;2;0.9135;1.4135;1
2012-04-24;2;0.9135;1.4135;3
2016-04-24;2;0.9135;1.4135;3
2016-04-24;2;0.9135;1.4335;3
2013-04-24;1;0.9135;1.4135;7
2011-04-24;2;0.9135;1.4135;1

I another post I found the following shell script which looks promising

awk -F';' 'NR==FNR{a[$4]=$1;next}{$5=a[$5]}1' lookup.txt data.txt

But the result below leaves for some reason column 5 blank for row 5,6,7 and 9. Why? The fact that we get space separators rather than ";" is also an issue but not as important

2013-04-24 1 0.1635 1.4135 1
2013-04-24 1 0.9135 1.4135 7
2017-04-24 2 0.9135 1.4135 1
2011-04-24 2 0.9135 1.4135 1
2012-04-24 2 0.9135 1.4135
2016-04-24 2 0.9135 1.4135
2016-04-24 2 0.9135 1.4335
2013-04-24 1 0.9135 1.4135 7
2011-04-24 2 0.9135 1.4135

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卸妝后依然美 2025-02-06 06:45:50

您可以使用此awk解决方案:

awk '
BEGIN{FS=OFS=";"}
{
   sub(/\r$/, "")
} 
NR == FNR {
   map[$NF] = $1
   next
}
{
   $NF = map[$NF]
} 1' lookup.txt data.txt

2013-04-24;1;0.1635;1.4135;1
2013-04-24;1;0.9135;1.4135;7
2017-04-24;2;0.9135;1.4135;1
2011-04-24;2;0.9135;1.4135;1
2012-04-24;2;0.9135;1.4135;3
2016-04-24;2;0.9135;1.4135;3
2016-04-24;2;0.9135;1.4335;3
2013-04-24;1;0.9135;1.4135;7
2011-04-24;2;0.9135;1.4135;1

说明:

  • 开始{fs = ofs =“;”}:设置输入和输出字段定界符到;
  • 使用nf一致而不是两个文件中的编号列
  • sub(/\ r $/,“”)是从行端删除任何运输字符

You may use this awk solution:

awk '
BEGIN{FS=OFS=";"}
{
   sub(/\r$/, "")
} 
NR == FNR {
   map[$NF] = $1
   next
}
{
   $NF = map[$NF]
} 1' lookup.txt data.txt

2013-04-24;1;0.1635;1.4135;1
2013-04-24;1;0.9135;1.4135;7
2017-04-24;2;0.9135;1.4135;1
2011-04-24;2;0.9135;1.4135;1
2012-04-24;2;0.9135;1.4135;3
2016-04-24;2;0.9135;1.4135;3
2016-04-24;2;0.9135;1.4335;3
2013-04-24;1;0.9135;1.4135;7
2011-04-24;2;0.9135;1.4135;1

Explanation:

  • BEGIN{FS=OFS=";"}: Sets input and output field delimiters to ;
  • Use NF consistently instead of numbered column in both files
  • sub(/\r$/, "") is to remove any carriage characters from line end
谢绝鈎搭 2025-02-06 06:45:50

通过在fnr == nr步骤上取消nf,它具有与使用Next语句的明确效果相同的效果。

如果您非常确定替换值永远不会为零,那么最终条件可以简化为$ nf = __ [$ nf]

mawk 'FNR==NR { NF=_*(__[$NF]=$!_) }_!~($NF=__[$NF])' RS='[\r]?[\n]' 
                                                   FS=';'
                                                  OFS=';' test_lookup_0005.txt 
                                                          test_data_0005.txt

2013-04-24;1;0.1635;1.4135;1
2013-04-24;1;0.9135;1.4135;7
2017-04-24;2;0.9135;1.4135;1
2011-04-24;2;0.9135;1.4135;1
2012-04-24;2;0.9135;1.4135;3
2016-04-24;2;0.9135;1.4135;3
2016-04-24;2;0.9135;1.4335;3
2013-04-24;1;0.9135;1.4135;7
2011-04-24;2;0.9135;1.4135;1

By nullifying NF at the FNR==NR step, it has the same effect as explicitly using next statement.

If you're very certain the replacement values are never zero, then the final condition can be simplified to just $NF = __[$NF]

mawk 'FNR==NR { NF=_*(__[$NF]=$!_) }_!~($NF=__[$NF])' RS='[\r]?[\n]' 
                                                   FS=';'
                                                  OFS=';' test_lookup_0005.txt 
                                                          test_data_0005.txt

2013-04-24;1;0.1635;1.4135;1
2013-04-24;1;0.9135;1.4135;7
2017-04-24;2;0.9135;1.4135;1
2011-04-24;2;0.9135;1.4135;1
2012-04-24;2;0.9135;1.4135;3
2016-04-24;2;0.9135;1.4135;3
2016-04-24;2;0.9135;1.4335;3
2013-04-24;1;0.9135;1.4135;7
2011-04-24;2;0.9135;1.4135;1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文