【问题标题】:Find unmatched list from directory files in unix从 unix 中的目录文件中查找不匹配的列表
【发布时间】:2020-07-29 15:21:17
【问题描述】:

我有一个名为 samples.list 的文件,其中包含示例 ID。我的目录中有相同的文件,我想与我的 sample.list 进行模式匹配并获得不匹配的 sample.list 的输出。

samples.list

SRR1369385 
SRR1352799
SRR1377262
SRR1400622  

ls -lh

-rw-rw----+ 1 gen dbgap_6109 2.2G Jul 29 02:44 SRR1369385_1.fastq.gz 
-rw-rw----+ 1 gen dbgap_6109 2.2G Jul 29 02:44 SRR1369385_2.fastq.gz 
-rw-rw----+ 1 gen dbgap_6109 1.2G Jul 29 03:34 SRR1352799_1.fastq.gz 
-rw-rw----+ 1 gen dbgap_6109 1.2G Jul 29 03:34 SRR1352799_2.fastq.gz 
-rw-rw----+ 1 gen tnt_pipeli 2.2G Jul 29 01:44 sometxt.txt

我想要的输出(与目录中文件名不匹配的样本):

SRR1377262
SRR1400622  

我试过的代码:

grep -oFf `cat samples.list` ls -lh | grep -vFf - `cat samples.list`

如果有人能指导我完成解决方案,我将不胜感激。

【问题讨论】:

  • @KamilCuk 我试过了,但是不行: grep -oFf `cat samples.list` $(ls -1q | tr " " "?") | grep -vFf - `cat samples.list`
  • 不解析ls。忘记 ls 的存在。 ls 用于在终端中打印漂亮的文件。如果您在脚本中,请不要使用ls。您正在尝试的行 grep -oFf cat samples.list $(ls -1q | tr " " "?") | grep -vFf - cat samples.list 意义不大,我建议您重新阅读相关的手册页。 :/我建议,不要“猜测”编程,而是先阅读然后尝试。
  • 在 SRR*.fastq.gz 文件所在目录运行join -v2 <(f=(*_*.fastq.gz); IFS=$'\n'; echo "${f[*]%%_*}" | uniq) <(sort -u /path/to/samples.list)

标签: bash shell unix awk sed


【解决方案1】:
# find all files named in the way you want and print filenames
find . -maxdepth 1 -type f -name '*_*.fastq.gz' -printf "%f\n" |
# Remove all everything except the SRR=numbers
sed 's/_.*//' |
# Sort the list, remove duplicate elements
sort -u |
# join the list with samples and print only unmatched elements from samples
join -v1 -o 1.1 <(sort samples.list) -

Tested on repl.

注意事项:

  • 不要使用反引号`。更喜欢$(...)obsolete and deprecated syntax bashhackers wiki
  • greps -f 选项采用 filename 而不是文件的内容。您可以使用grep -f some_file.txt 对存储在some_file.txt 中的所有正则表达式进行grep。
  • ls -lh 产生输出到stdout。运行grep ls -lh 将使grep 想要搜索名为ls 的文件(如果你想搜索文件名,-l-h 怎么办?)。虽然你可以ls -1 | grep,但最好find . -maxdepth 1 -mindepth 1 | grep ...

【讨论】:

    【解决方案2】:

    试试这个:

    awk -F_ 'NR==FNR{a[$1]=1;next}!($0 in a)' <(ls) samples.list
    

    首先它将为每个输出行从ls 索引直到_ 的所有内容(NR==FNR 对于这些行是正确的),然后在samples.list 中查找所有不匹配的行(»如果没有索引行,打印出来«)。

    【讨论】:

      猜你喜欢
      • 2018-06-08
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      • 2017-12-21
      • 1970-01-01
      • 2014-01-20
      • 2013-01-07
      • 2017-08-22
      相关资源
      最近更新 更多