0

I have the following file "nbr_chk.txt" ,this file contains numbers that can be in any directory (from 1 to 10). the sub directory where they are is the 5th and 6th number

nbr_chk.txt 
612345678
623456789
634567890

I want to make a script using that file and do the following:

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

edited 1:

For example the number 612345678 could be in the following directory /05/45/612345678 but could be also in the directory /09/45/612345678.

For that reason I need to do a ls -lrtd /*/.... to find the directory.

In case there are more than one directory I need to create an error message

d5 means the 5th digits and d6 the 6th digits of the number . if the number is 612300012 digit 5 =0 and digit 6=0 and the I would have to use ls -lrtd /*/00/612300012


If it was in an other language than Unix I would know how to do it but here I'm lost.

thanks

BigBen59
  • 123
  • 3
  • Please explain what you mean with; ① "a number can be in any directory (from 1 to 10)", ② "the sub directory is the 5th number", ③ "if there is more than one directory" — where? What about if there are several subdirectories _and_ a file containing the target? – Alfe Jan 21 '14 at 08:15
  • For example the number 612345678 could be in the following directory /05/45/612345678 but could be also in the directory /09/45/612345678. For that reason I need to do a ls -lrtd /*/.... to find the directory. – BigBen59 Jan 21 '14 at 08:29
  • Please explain what you mean by "In the following directory". People here don't like guessing games; this will make your question much less attractive, so you should come up with a decent description at least, otherwise you might not get an answer in the end. – Alfe Jan 21 '14 at 08:32

2 Answers2

0

The specification isn't too clear, but I take some guesses and come up with this:

#!/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

This cannot solve all issues you've got because some are still unclear (to me at least). Please elaborate on the not-met specifics so I can fit them in the solution.

Alfe
  • 56,346
  • 20
  • 107
  • 159
0

I think you may be looking for something like this:

#!/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
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Did this solve your problem? If so, could you accept my answer so I get a lovely big green tick please? If not, please say what went wrong or didn't work so I (and others) can help you further. – Mark Setchell Jan 22 '14 at 17:26