Search if file exists for a file pattern stored in array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search if file exists for a file pattern stored in array
# 1  
Old 02-20-2015
Search if file exists for a file pattern stored in array

Hi experts,

I have two arrays one has the file paths to be searched in , and the other has the files to be serached.For eg
searchfile.dat will have
Code:
abc303
xyz123

i have to search for files that could be abc303*.dat or for that matter any extension . abc303*.dat.gz

The following code doesnt work if i give * , it works only if i give the exact file name to be searched in the searchfile.dat
Code:
#!/usr/bin/ksh
set -A filearray $(cat searchfile.dat)
fp1=/home/user/fp1
fp2=/home/user/fp2
fp3=/home/user/fp3
 
set -A filepath $fp1 $fp2 $fp3
i=0
j=0
 while [[ $i -lt ${#filearray[@]}  ]];do
 while [[ $j -lt ${#filepath[@]} ]];do
 found=0
 if [[ -a  "${filepath[$j]}/${filearray[$i]}"\* ]]; then
 echo ${filepath[$j]}/${filearray[$i]} "found"



Moderator's Comments:
Mod Comment Please use code tags!!

Last edited by 100bees; 02-20-2015 at 09:07 AM..
# 2  
Old 02-20-2015
Try the find command:
Code:
while [[ $i -lt ${#filearray[@]}  ]]
do
   find ${filepath[*]} -type f -name ${filearray[$i]}\* | xargs ls -l 
   i=$(( $i + 1))
done

# 3  
Old 02-20-2015
Hi i tried with find and it says `${filepath[*]}' also is not expected.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If file pattern exists in directory then continue

he below looks in $dir for any pattern of fileone. As is, it executes but only returns File found if the exact format in the script exsists. Why isn't a pattern of fileone being looked for and if it is in $dir, File found. I think that is what should happen. Thank you :). dir=/path/to if... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

How to search a filename stored in a variable using a pattern?

hi, i have a variable which contains some file names delimited by a single space. FNAME="s1.txt s2.lst s3.cvs s4.lst" i have another variable that contains a pattern FILE_PATTERN="*.lst" i want to take the filenames from FNAME variable and assign each file name in to an array say for... (8 Replies)
Discussion started by: Little
8 Replies

3. Shell Programming and Scripting

Search for a pattern that exists in the codebase

Hello everyone, I want to search for a specific pattern in the entire codebase that exists. Pattern: I need to find wherever there is a harcoded value "Vxxxx" and "GCxxxx" exists in the perl scripts and shell scripts. (where xxxx will be always number) I need the script name and the value... (1 Reply)
Discussion started by: filter
1 Replies

4. Shell Programming and Scripting

Check for Pattern if exists write to file

Hi ! All I just want to search and write to new file if pattern is found in text file following are my text files by which I want to search Month and last column number my text file1 15-Jan-2011 25 ARTS 1255 125 125 178 198 15-Jan-2011 25 ARTS 1255 125 125 178 198 15-Jan-2011 25... (3 Replies)
Discussion started by: nex_asp
3 Replies

5. Shell Programming and Scripting

Want to Insert few lines which are stored in some file before a pattern in another file

Hello, I have few lines to be inserted in file_lines_to_insert. In another file final_file, I have to add lines from above file file_lines_to_insert before a particular pattern. e.g. $ cat file_lines_to_insert => contents are abc def lkj In another file final_file, before a... (6 Replies)
Discussion started by: nehashine
6 Replies

6. UNIX for Dummies Questions & Answers

Searching for a pattern from filenames stored in a file

Hi, I have got some 10 filenames stored in a file or displayed in the console as a result of some query i made.. Now I need to open each of these files and search for a pattern in these 10 files.. Can someone help me with this? Thanks, Jean (9 Replies)
Discussion started by: jeanjkj
9 Replies

7. UNIX for Dummies Questions & Answers

Need help finding a file where a pattern exists and the file has a timestamp

So, I know how to do some of this stuff on an individual level, but I'm drawing a blank as to how to put it all together. I have a pattern that I'm looking for in a log file. The log file I know came in yesterday, so I want to limit the search to that day's listing of files. How would I do... (5 Replies)
Discussion started by: kontrol
5 Replies

8. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

9. Shell Programming and Scripting

check if file exists with pattern matching

Hello friends, I am writing a simple shell script which will copy one particular type of files to backup folder if files exists. If files doesn't exists, mv command should not be executed. My file pattern is like wcm-spider-maestro.log.2009-07-15, wcm-spider-maestro.log.2009-07-16 etc.. I... (6 Replies)
Discussion started by: sreenu.shell
6 Replies

10. Shell Programming and Scripting

Prase a file and stored the result to an array

Dear all , I have a file whose content has the following format: jboss.web:type=ThreadPool,name=AAAA jboss.web:type=ThreadPool,name=BBBB How can I parse this file to get the value of the name of each line (AAAA , BBBB) and store the result to an array ? (array = AAAA , array = BBBB).... (4 Replies)
Discussion started by: youareapkman
4 Replies
Login or Register to Ask a Question