Operating System - HP-UX
1752569 Members
5041 Online
108788 Solutions
New Discussion юеВ

Re: find files from the list

 
syedar
Advisor

find files from the list

Hi,
I have a list contains values
50174214
52276142
52276142
52276498
52276869
52277263

File format:AB_20090116063203.50174214.Z
ID:50174214

Need to find files from the list which should match with ID.
9 REPLIES 9
Suraj K Sankari
Honored Contributor

Re: find files from the list

Hi,
I think you have a data file which is having data like
AB_20090116063203.50174215.Z
AB_20090116063203.50174224.Z
AB_20090116063203.50174214.Z
AB_20090116063203.50174264.Z
AB_20090116063203.52276142.Z
AB_20090116063203.50174214.Z

now you wana to search some string as you say
then you can use below awk program

awk '{
str=substr($0,19,8)
if (str==50174214 || str==50174224 || str==50174264) print $0
}'
Suraj
syedar
Advisor

Re: find files from the list

Hi suraj,

Yes you are right i have a data files but i extracted ID's from the data files and put it in the list.

Data files:
AB_20090116063203.50174214.Z
AB_20090116063203.52276142.Z
AB_20090116063203.52276142.Z
AB_20090116063203.52276498.Z
AB_20090116063203.52276869.Z
AB_20090116063203.52277263.Z

list(which already i have):
50174214
52276142
52276142
52276498
52276869
52277263

now i just want to find the files from another location (/A/test/) which should match the ID's in the list.


Suraj K Sankari
Honored Contributor

Re: find files from the list

Hi,
Then just add your input file location
awk '{
str=substr($0,19,8)
if (str==50174214 || str==50174224 || str==50174264) print $0
}'
if you wana take the output into another file then give like this at list line

}' /tmp/newdata
syedar
Advisor

Re: find files from the list

Hi,

Can we use find command for the same task.

first we should match the data files with list ID's and display the result.
find /A/test -type f -name "AB*" -exec ls -ltr {} \;
syedar
Advisor

Re: find files from the list

Hi all,

Please check this script geetting error ./Find.sh[7]: syntax error at line 2 : `print' unexpected

#!/bin/ksh
for rf in `ls -1 /A/test/AB*`
do
str=`echo $rf|cut -d"." -f2`
echo $str
awk `{
if (str==50174214) print $rf
}`done



Dennis Handly
Acclaimed Contributor

Re: find files from the list

>Please check this script getting error ./Find.sh[7]: syntax error at line 2 : `print' unexpected

You are using archaic ``, use $() instead. And `` instead of '':

for rf in $(ls /A/test/AB*); do
str=$(echo $rf | cut -d"." -f2)
echo $str
awk -v str="$str" -v rf="$rf" '
{
if (str == $1) print rf
}' data
done

Note this will correct the script.

You may want to use awk to build your ls command:
cd /A/test
ls -1 $(awk '
{
print "AB*" $1 ".Z"
}' /path-to/data 2> /dev/null

This may return a bunch of not found files.
Hein van den Heuvel
Honored Contributor

Re: find files from the list

What is the real problem you are trying to solve?
The 'right' solution depends on what you are going to do with the matching files. Just print the names?
The right solution also depends on the 'orders-of-magnitude' of the search list versus target files list. Both small (less than 100?), One small one large?

Here is a direction for a solution in PERL. Simlar solutions can be create in a shell of course:

perl -ne 'chomp; $f{$_}++} { for (<*.Z>) { if (/\.(\d+)\.Z/) { print qq($_\n) if $f{$1}}}' list.txt

-ne # for each input line
chomp;$f{$_}++ # remove newline and add as key to an array %f
} { # close input loop and open end block
for (<*.Z>) { # look through filename glob
if (/\.(\d+)\.Z/) # looks close?
{ print qq($_\n) if $f{$1} # print if in array
}
}'
Matthias Zander
Advisor

Re: find files from the list

If you wan to use find and your list of IDs is as short as in your example, you can go this way:
---------------------------------------------
#!/bin/ksh
LIST=/tmp/list.txt # List of IDs
SEARCHDIR=/tmp/dir/ # Dir to search
PREFIX=AB_??????????????.
POST=.Z

FINDCMD=""
cat $LIST | \
while read ID
do
if [ "$ID" = "" ]; then next; fi
if [ "$FINDCMD" != "" ]; then FINDCMD="$FINDCMD -o"; fi
FINDCMD="$FINDCMD -name $PREFIX$ID$POST"
done
if [ "$FINDCMD" = "" ]; then echo "list $LIST was empty"; fi
find $SEARCHDIR -type f \( $FINDCMD \)
---------------------------------------------
this will create one find for all files you are looking for.
Arturo Galbiati
Esteemed Contributor

Re: find files from the list

HI,
why noy simply:
ls -l A/test/AB*Z | grep -f your-key-file

HTH,
Rgds,
Art