【问题标题】:Print file names if they don't match a pattern如果文件名与模式不匹配,则打印它们
【发布时间】:2015-10-21 12:23:28
【问题描述】:

我有很多包含数字列表的文本文件,隐藏在很多文件夹中。大多数文件/列表都是相同的,我正在寻找一种方法来找到那些不一样的。

列表应准确包含这些数字:

0
50
100
150
200
250
300
350

我正在寻找一种方法来打印到文本文件,文件名和文件路径不完全一样。

我尝试使用 awk、sed 和其他 shell 工具,但由于我对此很陌生,所以我惨遭失败。我希望得到一个带有一些解释的例子。

谢谢!

【问题讨论】:

  • 这到底是什么?从 0 到 350,8 行第 50 步?
  • 是的。所有(大多数)文件都包含完全相同的列表,但它们本身具有不同的文件名。

标签: shell awk sed


【解决方案1】:

如果文件应该是完全重复的,你知道两件事:它们的大小应该是 29 字节,它们的 md5sum 应该是 00c7dd845c7e87a1d1751566bd23ad61 - 因为

seq 0 50 350 | wc -c
seq 0 50 350 | md5sum

所以,只需搜索不同大小或不同 md5sum 的文件:

find . -not -size 29c
find . -size 29c -exec md5sum {} + \
    | grep -v ^00c7dd845c7e87a1d1751566bd23ad61 \
    | cut -f2 -d\*

【讨论】:

  • 2 个问题:文件名不同。这不影响md5吗?另外,我如何将所有不匹配的文件名和路径打印到单个文本文件?
  • @o_ren:文件名与 md5 无关。您是否尝试过运行命令?他们已经生成了不匹配文件的列表。
  • 我刚刚尝试运行您的命令。我可以在终端中看到“坏”文件的列表。我尝试在命令末尾添加 >>/path/to/badlist.txt 但 badlist.txt 文件仅显示列表中的最后一个文件:/
  • @o_ren:这是两个命令。第一行也需要重定向。
【解决方案2】:
RefFile=./ThisFile

find . -type f -exec cksum {} \; \
 | awk -v "Ref=$( cksum ${RefFile} )" '
     BEGIN { split( Ref, aRef); crc=aRef[1] }
     $1 != crc { print $3}
     '

将返回与引用文件不同的文件

【讨论】:

    【解决方案3】:
    awk '
      # NR is numbers of rows read global
      # FNR is numbers of rows read for each file 
      # NR==FNR is only true for the first file
      # store the lines from the first file in an array 
      # use next to skip the next condition
      NR==FNR { a[NR]=$0; next }
      # this part is only ran when NR!=FNR
      # check to see if the array value is equal the row we currently read:
      a[FNR]!=$0 { print FILENAME }
    ' checkfile fileA fileB fileC ...
    

    这将打印每次不同于checkfile 的文件名,但这可以通过以下方式解决:| uniq

    awk '
      NR==FNR { a[NR]=$0; next }
      a[FNR]!=$0 { print FILENAME }
    ' checkfile fileA fileB fileC | uniq
    

    如果行顺序无关紧要,您可以使用$0 作为数组的键:

    awk '
      NR==FNR { a[$0]=1; next }
      !($0 in a) { print FILENAME }
    ' checkfile fileA fileB fileC
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-04
      • 2018-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      相关资源
      最近更新 更多