繁体   English   中英

Bash 脚本检查 2 个新旧 csv 文件。 要在新文件中检查,行数的内容是旧文件的 x %?

[英]Bash script that checks between 2 csv files old and new. To check that in the new file, the line count has content which is x % of the old files?

到目前为止,我编写脚本的方式是计算 2 个文件的行数。

然后我把它放在条件如果它大于旧的。

但是,我不确定如何根据旧文件的百分比进行比较。

我有更好的方法来设计脚本。

#!/bin/bash

declare -i new=$(< "$(ls -t file name*.csv | head -n 1)" wc -l)
declare -i old=$(< "$(ls -t file name*.csv | head -n 2)" wc -l)

echo $new
echo $old

if [ $new -gt $old ];
then
echo "okay";
else
echo "fail";

如果您需要检查 x% max diff 行,您可以计算 diff output 中的 '<' 行数。 回想一下差异 output 的样子。

+ diff node001.html node002.html
2,3c2,3
< 4
< 7
---
> 2
> 3

所以该代码将如下所示:

old=$(wc -l < file1)
diff1=$(diff file1 file2 | grep -c '^<')
pct=$((diff1*100/(old-1)))
   # Check Percent
if [ "$pct" -gt 60 ] ; then
   ...
fi

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM