【问题标题】:Find filenames matching lines in a text file在文本文件中查找与行匹配的文件名
【发布时间】:2020-03-03 00:31:57
【问题描述】:

我有一个文本文件如下:

orthogroup1
orthogroup4
...

我有一个包含如下文本文件的目录:

orthogroup1.faa.reconciled
orthogroup2.faa.reconciled
orthogroup3.faa.reconciled
orthogroup4.faa.reconciled
...

我想使用文本文件在我的目录中获取相应的文件名。

结果应该是这样的:

orthogroup1.faa.reconciled
orthogroup4.faa.reconciled

我该怎么做?

提前谢谢:)

【问题讨论】:

标签: unix


【解决方案1】:

你能定义“获取我的目录中的文件名”是什么意思吗?你对他们有什么下一步计划?

似乎您可能想使用sed 或awk 之类的东西,尽管后者可能有点矫枉过正。请参阅What is the difference between sed and awk? 了解更多信息。

【讨论】:

    【解决方案2】:

    grep 的-f 选项允许您从文件中获取模式。在find 的输出上使用它来过滤掉列表:

    find /path/to/dir -type f | grep -f /path/to/patterns
    

    【讨论】:

    • 啊,好吧,我以为这就是你想要的。
    • 好吧,如果您可以控制要匹配的文件列表,您可以编辑(或生成)它,以便每行以\. 结尾,如orthogroup1\. 等等。这将匹配第一个点分隔符。
    • 你知道我现在如何将上面命令的输出复制到一个新的/不同的目录吗?非常感谢你:)
    • 如果您指的是文本列表,只需使用重定向运算符:find /path/to/dir -type f | grep -f /path/to/patterns > /path/to/list/of/matches
    • 我的目标是移动实际文件,这可能吗?
    【解决方案3】:

    类似这样的:

    suffix='.faa.reconciled'
    for line in $(cat file.txt); do
        echo $line$suffix
    done
    

    例子:

    $ cat file.txt
    orthogroup1
    orthogroup4
    
    $ ls -l ortho*
    orthogroup4.faa.reconciled
    orthogroup3.faa.reconciled
    orthogroup2.faa.reconciled
    orthogroup1.faa.reconciled
    
    $ suffix='.faa.reconciled'; for line in $(cat file.txt); do echo $line$suffix; done
    orthogroup1.faa.reconciled
    orthogroup4.faa.reconciled
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      • 2016-11-23
      • 1970-01-01
      • 2018-03-12
      • 2022-12-11
      • 1970-01-01
      相关资源
      最近更新 更多