简体   繁体   English

是否可以使用 bash cmd 获取 file1 减去 file2 的内容?

[英]is it possible to get the content of file1 minus file2 by using bash cmd?

I have two files:我有两个文件:

log.txt 
log.bak2022.06.20.10.00.txt

the log.bak2022.06.20.10.00.txt is the backup of log.txt at 2022.06.20 10:00 . log.bak2022.06.20.10.00.txtlog.txt2022.06.20 10:00的备份。

but the log.txt is a content-increasing file.log.txt是一个增加内容的文件。

now I have a requirement, I want get the content of log.txt minus log.bak2022.06.20.10.00.txt , then write into a new file.现在我有一个要求,我想得到log.txt减去log.bak2022.06.20.10.00.txt的内容,然后写入一个新文件。 is it possible to implement it?有可能实施吗?

Assumptions:假设:

  • the small file contains N lines, and these N lines are an exact match for the 1st N lines in the big file小文件包含 N 行,这 N 行与大文件中的第一个 N 行完全匹配

Sample inputs:样本输入:

$ cat small
4
2
1
3

$ cat big
4
2
1
3
8
10
9
4

One comm idea:一个comm的想法:

$ comm --nocheck-order -13 small big
8
10
9
4

One awk idea:一个awk的想法:

$ awk '
FNR==NR { max=FNR; next }
FNR>max
' small big
8
10
9
4

One wc/sed idea:一个wc/sed的想法:

$ max=$(wc -l < small)
$ ((max++))
$ sed -n "$max,$ p" big
8
10
9
4

awk -based solution without need for unix piping |基于awk的解决方案,无需 unix | chains, regex , function calling, or array splitting :链、 regex 、函数调用或array拆分:

{m,n,g}awk '(_+= NR==FNR ) < FNR' FS='^$' small.txt big.txt 

8
10
9
4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 shell:通过FILE2中的内容从FILE1中获取行 - shell: Get line from FILE1 by content in FILE2 sed用file1中的file3内容替换file2内容 - sed replace file2 content with file3 content in file1 bash-根据file2中的ID查找file1中的名称 - bash - Find names in file1 according to IDs in file2 bash 命令在文件 2 中的特定字符串/文本之后将文件内容从文件 1 复制到文件 2 - bash command to copy file contents from file1 to file2 after particular string/text in file2 使用循环从file2中提取存在于file1中的行 - Extract lines from file2 that exist in file1 using a loop cat 多个文件在单独的目录 file1 file2 file3....file100 在 bash 脚本中使用循环 - cat multiple files in separate directories file1 file2 file3....file100 using loop in bash script 当file1包含额外信息时,使用file1作为索引来搜索file2 - Using file1 as an Index to search file2 when file1 contains extra informations 如何使用Bash-嵌套循环查看file1列中的数字是否在file2列中的数字之间? - How to see if number in column from file1 is between numbers from columns in file2 using Bash - nested loops? 将文件 1 中的字符串与文件 2 中的字符串匹配 - Match string in file1 with string in file2 Bash 检查 file1 行是否部分包含在 file2 的一行中 - Bash check if file1 line is partly contained in a line from file2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM