views:

72

answers:

4

I want to retrieve the file from the INFILE directory which are begining with the file names prefix "BBSCGG_" or "BCT_" or "ACL_" or "ASC" and do the processing inside the for loop

INFILE=/ext/test/fil1/

for infile name in file prefix

...  if [[ -f ${fspec} ]] ; then

            processing logic

     else
            processing logic
done  

how can i do it

+2  A: 
for name in "$infile"{BBSCGG_,BCT_,ACL_,ASC}*
do
  ....
done
Ignacio Vazquez-Abrams
If there are no prefix files in the directory i want to display a warning. how can i do it?
Arav
for loop will exit with failed status if no files are in the directory
Arav
I ran the program you pasted. It's not working. The input files are in the directory but not picking
Arav
Because there's a typo: substitute `$infile` with `$INFILE`.
wilhelmtell
Brace expansion works in bash and csh. Apparently ghostdog74 gave a command to make it work on ksh, but I don't know enough about it to advise you on that. You can use a sentinel to detect whether or not any files were processed. Set it to `0` before the loop, and then to `1` inside the loop. If it's still 0 after the loop then no files have been processed if `nullglobs` (or whatever the equivalent for ksh is) is on.
Ignacio Vazquez-Abrams
As for your question in the first comment, you can have another loop counting the files in each directory. `for name in BBSCGG_ BCT_ ACL_ ASC; do files="$(ls $INFILE/$name* 2>/dev/null)"; if [ -z "$files" ]; then echo "No files with prefix $name found in $INFILE"; fi; done`
wilhelmtell
+1  A: 
#!/bin/ksh

flag=0
set -o braceexpand
for file in {BBSCGG_,BCT_,ACL_,ASC_}*
do
  if [ -f "$file" ];then
     # do your stuff if there are files
     flag=1
  fi
done
if [ "$flag" -eq 0 ];then
    echo "warning. empty"
fi
ghostdog74
If there are no prefix files in the directory i want to display a warning. how can i do it? for loop will exit with failed status if no files are in the directory
Arav
you can use a flag or you can check the directory for files first. see edit
ghostdog74
I ran the program you pasted. It's not working. The input files are in the directory but not picking. i did a echo $file after the do statement and i am getting the below output. Not sure why it's not working. Also If there is no files also it's going inside the folder and setting the flag. I am using ksh so i cant do the shopt command that you mentioned in earlier posT. I am not sure why you want to do cd $infile before checking files ./test.sh{BBSCGG_,BCT_,ACL_,ASC_}*
Arav
then you make a test for file inside your loop. if no files are found with those patterns, the flag won't be set.
ghostdog74
+1  A: 

You may want to take a look at the "find" command too if subdirectories exist. Check this out first.

shinkou
Thanks a lot for the info
Arav
A: 
ls -1 $INFILE/{BBSCGG_,BCT_,ACL_,ASC}* |while read FILE; do
  # $FILE holds full pathname of each prefixed file.
  # mmk go ...
done

If you want all files in the tree under $INFILE then use find rather than ls:

find $INFILE -name BBSCGG_\* -o \
-name BCT_\* -o \
-name ACL_\* -o \
-name ASC\* |while read FILE; do
  # kthx
done
wilhelmtell
Thanks a lot for the info
Arav