bash - 如何逐行比较两个文件并在不同时输出整行
问题描述
我有两个排序的文件有问题
1)one is a control file(ctrl.txt) which is external process generated
2)and other is line count file(count.txt) that I generate using `wc -l`
$more ctrl.txt
Thunderbird|1000
Mustang|2000
Hurricane|3000
$more count.txt
Thunder_bird|1000
MUSTANG|2000
Hurricane|3001
我想比较这两个文件,忽略 column1(filenames) 中的皱纹,例如“_”(用于 Thunder_bird)或“大写”(用于 MUSTANG),以便我的输出仅显示下面的文件作为唯一真正不同的文件计数不匹配。
Hurricane|3000
我的想法是只比较两个文件中的第二列,如果它们不同则输出整行
我在 AWK 中看到了其他示例,但我无法得到任何工作。
解决方案
您能否尝试关注awk
并让我知道这是否对您有帮助。
awk -F"|" 'FNR==NR{gsub(/_/,"");a[tolower($1)]=$2;next} {gsub(/_/,"")} ((tolower($1) in a) && $2!=a[tolower($1)])' cntrl.txt count.txt
现在也添加非单线形式的解决方案。
awk -F"|" '
FNR==NR{
gsub(/_/,"");
a[tolower($1)]=$2;
next}
{ gsub(/_/,"") }
((tolower($1) in a) && $2!=a[tolower($1)])
' cntrl.txt count.txt
说明:这里也为上面的代码添加说明。
awk -F"|" ' ##Setting field seprator as |(pipe) here for all lines in Input_file(s).
FNR==NR{ ##Checking condition FNR==NR which will be TRUE when first Input_file(cntrl.txt) in this case is being read. Following instructions will be executed once this condition is TRUE.
gsub(/_/,""); ##Using gsub utility of awk to globally subtitute _ with NULL in current line.
a[tolower($1)]=$2; ##Creating an array named a whose index is first field in LOWER CASE to avoid confusions and value is $2 of current line.
next} ##next is awk out of the box keyword which will skip all further instructions now.(to make sure they are read when 2nd Input-file named count.txt is being read).
{ gsub(/_/,"") } ##Statements from here will be executed when 2nd Input_file is being read, using gsub to remove _ all occurrences from line.
((tolower($1) in a) && $2!=a[tolower($1)]) ##Checking condition here if lower form of $1 is present in array a and value of current line $2 is NOT equal to array a value. If this condition is TRUE then print the current line, since I have NOT given any action so by default printing of current line will happen from count.txt file.
' cntrl.txt count.txt ##Mentioning the Input_file names here which we have to pass to awk.
推荐阅读
- batch-file - cd 进入与 dir 一起使用的找到文件的目录
- python - 如何解决来自 Xpath 的此错误?
- python-3.x - 将数据框合并为一列中的 None 时省略重复项
- java - WebSocket STOMP 已经覆盖类主体方法 getname() convertAndSendToUser 不起作用
- java - 如果 Firebase 中的另一个子值匹配,如何获取子值?
- mongodb - 在 PSA 设置中的 oplog 中查找数据修改条目
- spring-boot - JSON解析错误:无法构造无字符串参数构造函数/工厂方法的实例以从字符串值(“名称”)反序列化
- ansible - ansible authorized_key 模块覆盖原始文件
- applepay - 如何在 Shopify 网站上自定义苹果支付按钮和谷歌支付按钮样式
- angular - SpringCloud Dataflow Keycloak Angular 8集成 - 401未经授权(有时(?))