【问题标题】:script for checking directories and file contains a word检查目录和文件的脚本包含一个单词
【发布时间】:2014-02-10 16:57:19
【问题描述】:

我有以下文件“nbr_chk.txt”,该文件包含可以在任何目录中的数字(从 1 到 10)。它们所在的子目录是第5和第6个数字

nbr_chk.txt 
612345678
623456789
634567890

我想使用该文件制作一个脚本并执行以下操作:

for i in 'cat nbr_chk.txt'
do
   ls -lrtd /*/d5d6/i   to find the directory
   if there is more than one directory  print  the directories   

   if there is only 1 directory use it and find if there is a file that contains the word  " test"  and print number xxxxxxxxx 


done

编辑 1:

例如号码 612345678 可能在以下目录 /05/45/612345678 但也可以在目录 /09/45/612345678 中。

因此,我需要执行 ls -lrtd /*/.... 来查找目录。

如果有多个目录,我需要创建一条错误消息

d5 表示数字的第 5 位,d6 表示数字的第 6 位。 如果数字是 612300012 数字 5 =0 和数字 6=0 并且我将不得不使用 ls -lrtd /*/00/612300012


如果它是 Unix 以外的其他语言,我会知道该怎么做,但在这里我迷路了。

谢谢

【问题讨论】:

  • 请解释一下你的意思; ①“一个数字可以在任何目录(从1到10)”,②“子目录是第5个数字”,③“如果有多个目录”——在哪里?如果有多个子目录一个包含目标的文件呢?
  • 例如号码 612345678 可能在以下目录 /05/45/612345678 中,但也可能在目录 /09/45/612345678 中。出于这个原因,我需要执行 ls -lrtd /*/.... 来查找目录。
  • 请解释您所说的“在以下目录中”是什么意思。这里的人不喜欢猜谜游戏;这会让你的问题变得不那么有吸引力,所以你至少应该想出一个像样的描述,否则你最终可能得不到答案。

标签: bash command cat


【解决方案1】:

规范不是很清楚,但我猜了几下,得出了这个:

#!/bin/bash

for i in $(cat nbr_chk.txt)
do
  dirName=d${i:4:1}d${i:5:1}
  grep -q " test" "$dirName"/* && echo "$i"
done

这不能解决您遇到的所有问题,因为有些问题仍然不清楚(至少对我而言)。请详细说明未满足的细节,以便我可以将它们纳入解决方案。

【讨论】:

    【解决方案2】:

    我想你可能正在寻找这样的东西:

    #!/bin/bash
    
    for i in $(cat nbr_chk.txt)
    do
       dir=d${i:4:1}d${i:5:1}
       echo Checking $dir...
       dirlist=$(find . -type d -name "$dir")
       ndirs=$(find . -type d -name "$dir" | wc -l)
       if [ $ndirs -gt 1 ]; then
          echo $dirlist
       fi
       if [ $ndirs -eq 1 ]; then
          cd $dirlist 
          grep -q " test" * 2> /dev/null && echo $i
       fi
    done
    

    【讨论】:

    • 这解决了您的问题吗?如果是这样,您能接受我的回答,以便我得到一个可爱的绿色大勾号吗?如果没有,请说出哪里出了问题或没有工作,以便我(和其他人)可以进一步帮助您。
    猜你喜欢
    • 2010-09-10
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    相关资源
    最近更新 更多