shell - 与 shell 脚本中元素位置的行差异
问题描述
输入:
文件1.txt
abc 1 2 3 4
文件2.txt
abc 1 2 5 6
预期输出:
differences is
3
5
at location 3
我可以使用以下方法跟踪差异:
comm -3 file1.txt file2.txt | uniq -c | awk '{print $4}' | uniq
但无法跟踪元素位置。
你们能否建议使用shell脚本来跟踪元素位置?
解决方案
为了方便起见,使用 perl 和 CPAN 中的 Path::Class
perl -MPath::Class -MList::Util=first -e '
@f1 = split " ", file(shift)->slurp;
@f2 = split " ", file(shift)->slurp;
$idx = first {$f1[$_] ne $f2[$_]} 0..$#f1;
printf "difference is\n%s\n%s\nat index %d\n", $f1[$idx], $f2[$idx], $idx;
' file{1,2}.txt
difference is
3
5
at index 3
推荐阅读
- elasticsearch - FilterAggregationBuilder 类型中的方法 filter(FilterBuilder) 不适用于参数 (BoolQueryBuilder)
- vim - 添加 .vimrc 后 vim81 配置错误
- reactjs - Redux 表单从状态初始化表单
- html - 一种 CSS 样式仅适用于本地主机而不适用于服务器托管
- javascript - 在Javascript中调用带参数的函数和OnClick触发不带参数的函数之间的区别?
- php - 使用 Phalcon 登录 Facebook 中的重定向过多
- reactjs - 类型上不存在属性“onload”
- java - URL 中的 % 重定向到 HTTP ERROR 400 而不是默认错误页面
- c# - 如何将字符串属性转换为布尔值?
- c# - 解析 "?<=Sys ).*" - 量词 {x,y} 什么都不做