- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一系列 .csv 文件,其中包含由空格分隔的柱状(5 列)数据。文件名的格式为“yyyymmdd.csv”。文件格式举例如下:
20161201.csv的内容
key value more columns (this line (header) is absent)
123456 10000 some value
123457 20000 some value
123458 30000 some value
20161202.csv内容
key value more columns (this line (header) is absent)
123456 10000 some value
123457 80000 some value
123458 30000 some value
20161203.csv的内容
key value more columns (this line (header) is absent)
123456 50000 some value
123457 70000 some value
123458 30000 some value
我想根据值列将日期为“D”的文件与日期为“D+1”的文件进行比较。然后我对最大行数不同的两个连续文件感兴趣。所以像这里一样,如果我将 20161201.csv 与 20161202.csv 进行比较,我只会得到第二行不匹配
(123457 20000 一些值和 123457 80000 一些值,因为 20000 不匹配!= 80000)
然后如果我将 20161202.csv 与 20161203.csv 进行比较,我会得到 2 行不匹配(第一行和第二行)
因此,20161202.csv 和 20161203.csv 是我的目标文件。
我正在寻找可以执行相同操作的一系列 bash 命令。
PS:文件中的行数很大(大约 3000 行),您可以假设所有文件具有相同的年份和月份(文件数<30)。
最佳答案
如果不检查文件名是否符合日期比较规则(数据文件与日期+1 文件),您可以这样做:
while IFS= read -r -d '' fn;do files+=("$fn");done < <(find . -name '201612*.csv' -print0)
#Load all filenames in an array. Using null separation we ensure that filenames will be
#handled correctly no matter if they do contain spaces or other special chars.
max=0
for ((i=0;i<"${#files[@]}"-1;i++));do #iterate through the filenames array
a="${files[i]}";b="${files[i+1]}" #compare file1 with file2, file2 with file3, etc - in series
differences=$(grep -v -Fw -f <(cut -d' ' -f2 "$a") <(cut -d' ' -f2 "$b") |wc -l)
echo "comparing $a vs $b - non matching lines=$differences" #Just for testing - can be removed .
[[ "$max" -lt "$differences" ]] && max="$differences" && ahold="$a" && bhold="$b" #When we have the max differences we keep the names of the files
done
echo "max differences found=$max between $ahold and $bhold" #reporting max differences and in which files found
获取两个文件之间不匹配行的核心是grep。您可以手动尝试 grep 以查看结果是否正确:
grep -v -F -w -f <(cut -d' ' -f2 file1) <(cut -d' ' -f2 file2)
grep 选项:
-v :返回不匹配的行(grep的逆操作)
-F : 固定-不是正则表达式-匹配
-w : 单词匹配,避免 5000 与 50000 匹配
-f :从文件加载模式,特别是从文件 1、字段 2。使用此模式,我们将 grep/搜索文件 2 的字段 2。
wc -l :计算匹配项 = 不匹配的行<(cut -d' ' -f2 file2) : 我们 grep file2 的 field2 而不是整个 file2 以避免 file2 的其他列中 file1/field2 的可能匹配而不是 column2
代替 grep ,您可以使用这样的 awk:
awk 'NR==FNR{a[$2];next}!($2 in a)' file1 file2
这将打印与 grep -v
file1/field2($2) 将加载到数组 a
将打印不在此数组中的 file2/field2 ($2) 行(非匹配字段)。
也可以通过管道传输到 |wc -l
来计算不匹配的行数,就像在 grep 中一样。
所以如果你更喜欢使用 awk,这一行:
differences=$(grep -v -Fw -f <(cut -d' ' -f2 "$a") <(cut -d' ' -f2 "$b") |wc -l)
必须改为:
differences=$(awk 'NR==FNR{a[$2];next}!($2 in a)' $a $b |wc -l)
无论如何,您似乎需要一个数组来保存文件名,然后您需要一个循环来遍历文件并成对比较它们。
关于linux - 如何获得一系列文件中具有最大差异的两个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42415826/
“用 Haskell 进行函数式思考”中的练习之一是使用融合定律使程序更加高效。我在尝试复制答案时遇到了一些麻烦。 部分计算要求您将 maximum (xs++ map (x+) xs) 转换为 ma
我正在尝试获得 R 中最大/最小的可表示数字。 输入“.Machine”后 我有: $double.xmin [1] 2.225074e-308 $double.xmax [1] 1.797693e+
有没有办法更改浏览器验证消息 请检查所附图片。 我目前正在使用 wooCommerce 目前它显示小于或等于 X 个数字,我想更改为请求超过 X 个项目的报价。 请多多指教 最佳答案 您需要使用oni
我正在尝试将解决方案从 Excel 求解器复制到 R 中,但不知道从哪里开始。 问题: 每小时选择 5 个选项(5 行),以最大化“分数”的总和,而无需在多个小时内选择同一组 2 次。 换句话说: 最
Haskell 中是否有这样的功能: max_of_type :: (Num a) => a 所以: max_of_type :: Int == 2 ^ 31 - 1 // for example,
我有这两个表示时间范围(秒)的输入字段,我需要这样设置,以便“from/min”字段不能高于“to/max”,反之亦然。 到目前为止我得到了这个: jQuery(document).ready(fun
我有一个看起来像这样的表: http://sqlfiddle.com/#!9/152d2/1/0 CREATE TABLE Table1 ( id int, value decimal(10,
我会尝试尽可能简单地解释它: 首先是一些带有虚拟数据的数据库结构。 结构 tb_spec_fk feature value ----------------- 1 1 1
我有两个表。 表 1: +---------+---------+ | Lead_ID | Deal_ID | +---------+---------+ | 2323 | null |
我的数据库中有一个字段可以包含数字,例如8.00 或范围编号,例如8.00 - 10.00。 如果您将每个数字作为单独的数字,我需要从表中获取 MIN() 和 MAX()。例如当范围为 8.00 -
max(float('nan'), 1) 计算结果为 nan max(1, float('nan')) 计算结果为 1 这是预期的行为吗? 感谢您的回答。 max 在 iterable 为空时引发异常
我想问一下如何在 CSS 中创建一个页脚栏,它具有最小宽度(比如 650 像素),并且会根据窗口大小进行拉伸(stretch),但仅限于某个点(比如 1024 像素)。 我的意思是当窗口大小为例如 1
我尝试调整表格列宽(下一个链接上的“作者”列 http://deploy.jtalks.org/jcommune/branches/1?lang=en)。我已将最小/最大属性添加到 .author-c
在 C# 中,是否有用于将最小值和最大值存储为 double 值的内置类? 此处列出的要点 http://msdn.microsoft.com/en-us/library/system.windows
问题: 每个任务队列是否可以每秒处理超过 500 个任务? 每个 GAE 应用是否可以每秒处理超过 50,000 个任务? 详细信息: Task queue quota文档说: Push Queue
我想知道是否允许最大或最小堆树具有重复值?我试图仅通过在线资源查找与此相关的信息,但一直没有成功。 最佳答案 是的,他们可以。您可以在“算法简介”(Charles E. Leiserson、Cliff
首先,我是 .NET 开发人员,喜欢 C# 中的 LINQ 和扩展方法。 但是当我编写脚本时,我需要相当于 Enumerable extension methods 的东西 任何人都可以给我任何建议/
这是一个检查最大 malloc 大小的简单程序: #include std::size_t maxDataSize = 2097152000; //2000mb void MallocTest(vo
我想找到我的数据的最小值和最大值。 我的数据文件: 1 2 4 5 -3 -13 112 -3 55 42 42 而我的脚本: {min=max=$1} {if ($1max) {max=$1}
我想查询我的Elastic-Search以获取仅具有正值的最低价格价格。我的价格也可以为零和-1;所以我不希望我的最小聚合返回0或-1。我知道我应该向查询(或过滤器)添加脚本,但是我不知道如何。我当前
我是一名优秀的程序员,十分优秀!