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

标签 r bash awk merge

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

文件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 语言中获得解决方案。我在网上搜索了解决方案,虽然有很多*(请在下面找到一些示例),但无论它们位于文件内部还是外部,所有这些都删除了重复的行。

提前致谢。 阿图罗

最佳答案

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

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.

关于r - 合并和删除*文件之间的冗余行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66888026/

相关文章:

r - 绘制时如何在 R 中隐藏 TclTk 窗口

r - 使用 'render_book' 时忽略输出选项 ('preamble' .tex' 被忽略)

xml - 使用sed有条件地删除xml文件中的行

bash oneliner 将 *.ext 文件移动到新创建的目录中

awk - 正则表达式使用 awk 仅打印匹配的字符串而不是整行

r - 如何测试 R 对象是否是命名的数值向量?

r - 如何使用r中的for循环使用先前的观察来预测下一个时期?

linux - 命令行参数的Shell脚本密码安全

shell - 每组打印前N行->删除所有docker镜像,每个镜像名称的最新N个除外

linux - 在 AWK 中去除空格