我的输出diff
格式如下:
$ curr_dt=$(date +%m_%d_%Y)
$ echo $curr_dt
$ diff -q /POC/$curr_dt/DDL/ /POC/Backup/DDL/
Only in /POC/Backup/DDL/: comp1.txt
Only in /POC/Backup/DDL/: comp.txt
Only in /POC/Backup/DDL/: junk.txt
Files /POC/$curr_dt/DDL/test.demo_table1.txt and /POC/Backup/DDL/test.demo_table1.txt differ
Only in /POC/$curr_dt/DDL/: test.demo_table2.txt
我现在想从上面显示的数据中得到以下信息:
- 仅在/POC/Backup/DDL/中:
- 仅在 /POC/$curr_dt/DDL/ 中:
- 文件 * 不同
例如:
diff -q /POC/05_26_2022/DDL/ /POC/Backup/DDL/ | grep -i '^Files.*.differ$'
Files /POC/05_26_2022/DDL/test.demo_table1.txt and /POC/Backup/DDL/test.demo_table1.txt differ
像上面一样,我得到了一整行数据。我只需要从中获取 test.demo_table1.txt 并将其保存在文件中。同样地
i am not able to grep with variable $curr_dt in the file
答案1
看起来您的$curr_dt
变量未在上面给出的代码片段中设置/未扩展。至少它没有echo
也没有出现在diff
的输出中。使用-x
(xtrace) 选项集运行代码。对于您的其他问题,请尝试
awk '/^Only/ {gsub("[/:]+", "_", $3); print $NF > $3} /^Files.*differ/ {sub(/^.*\//, "", $2); print $2 > "different"}' file
它应该创建三个文件,其中包含各自的文件名。
根据OP的重复请求,文件名缩减为已识别/识别目录:
awk '
/Only/ {m = split($3, T, "[/:]+")
print $NF > T[m-2]
}
/Files.*differ/ {sub(/^.*\//, "", $2)
print $2 > "different"
}
' file