r - 合并和删除*文件之间的冗余行

我需要合并多个文件,删除 文件中的冗余行,同时保留 文件中的冗余行。我的文件的示意图如下:

文件1.txt

1
2
3
3
4
5
6

文件2.txt

6
7
8
8
9

文件3.txt

9
10
10
11

期望的输出是:

1
2
3
3
4
5
6
7
8
8
9
10
10
11

我更愿意在 awk、bash 或 R 语言中获得解决方案。我在网上搜索了解决方案,虽然有很多*(请在下面找到一些示例),但无论它们位于文件内部还是外部,所有这些都删除了重复的行。

提前致谢。 阿图罗

  • 以前的解决方案示例,用于删除文件内外的冗余行: https://unix.stackexchange.com/questions/50103/merge-two-lists-while-removing-duplicates https://unix.stackexchange.com/questions/457320/combine-text-files-and-delete-duplicate-lines https://unix.stackexchange.com/questions/350520/awk-combine-two-big-files-and-remove-duplicated-lines https://unix.stackexchange.com/questions/257467/merging-2-files-and-keeping-the-one-duplicate

最佳答案

根据您展示的示例,您能否尝试以下操作。这不会删除文件中的冗余行,但会明智地删除它们。

awk '
FNR==1{
  for(key in current){
    total[key]
  }
  delete current
}
!($0 in total)
{
  current[$0]
}
' file1.txt file2.txt  file3.txt

说明: 为以上添加详细说明。

awk '                                ##Starting awk program from here.
FNR==1{                              ##Checking condition if its first line(of each file) then do following.
  for(key in current){               ##Traverse through current array here.
    total[key]                       ##placing index of current array into total(for all files) one.
  }
  delete current                     ##Deleting current array here.
}
!($0 in total)                       ##If current line is NOT present in total then do following.
{
  current[$0]                        ##Place current line into current array.
}
' file1.txt file2.txt  file3.txt     ##Mentioning Input_file names here.

https://stackoverflow.com/questions/66888026/

相关文章:

html - 将样式添加到 mat-autocomplete 的 mat-option

c++ - 为什么C++语言的malloc函数前面要写指针类型?

reactjs - 配置多个下一个插件 : withMDX, withBundleAnalyzer

regex - 为电话号码编写正则表达式

ios - React native Pod 安装得到权限被拒绝

git - 使用 git rev-parse 的错误提交哈希

email - SendGrid Automated Security 如何处理 SPF 记录?

c++ - 如果 new 运算符失败,是否可以将指针设置为 null?

.net-core - Cake Clean 任务在蛋糕构建时被跳过

postgresql - 我想用 mikro-orm 插入,但它找不到我的表 :c (TableNo