我需要合并多个文件,删除 文件中的冗余行,同时保留 文件中的冗余行。我的文件的示意图如下:
文件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.
关于r - 合并和删除*文件之间的冗余行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66888026/