How to compare file name with a string

Hi,

Suppose in some directory (/APPS/TEST) I have a file ABCTEST.csv. I want to write a IF condition in which I want to check if filename contains the string ABC then I will do certain operations .

I have a variable having value as /APPS/TEST/ABCTEST.csv

How Can I achieve this...please guide me.

Thanks

Using perl you can compare the file name

opendir DIR,"." or die "Can't open dir : $!\n";
while (my $file=readdir(DIR))
{
        if ($file=~/ABC/)
        {
            print "Found file : $file\n";
        }
}

I am having files named ABCDE and ABCJK and some other files.
So the code is now printing

Found file : ABCDE
Found file : ABCJK

---------- Post updated at 02:32 PM ---------- Previous update was at 02:22 PM ----------

Or in bash you can use the following way also

string=ABC
for file in `ls -l  /APPS/TEST | tr -s ' ' | sed '1d' | cut -d' ' -f 9`
do
        var=`expr index "$string" $file`
        if [ $var -gt 0 ]
        then
                echo "$file"
        fi
done

Simply with case/esac :

#!/bin/sh

for file in /APPS/TEST/*
do
  case "$file" in
     *ABC*) echo "$file"
	;;
    *)
	;;
  esac
done

Go to the directory /APPS/TEST/

 
ls -l | grep "ABC" 

Hi Franklin52,

The below script by you is working fine but how can I get the file name from variable "file" because here it would be a full path.

Thanks in advance
Vishalaksha

for file in /APPS/TEST/*
do
  case "$file" in
    *ABC*) 
       filename=${file##*/}
       echo "$filename"
       ;;
    *)
       ;;
  esac
done

path=/home/user1/pictures/car.jpg
echo ${path##*/}
Now this will print only the file name
car.jpg

Hi
I tried with this but I am getting some problem with file name.

Actually I am writing a script in which there would be some csv files in a particular directory. Suppose in directory there are two files ABC.csv and XYZ.csv. Now I want to load these two files to different database tables using SQL loader. Based on the file name I am passing the table name to SQL loader.
While running it I am gettng below errors:

Below is my script

Please guide me in finding the error.

Thanks in advance
Vishalaksha

A quick walk through, I found :

v_filename=${file##*/}

you have nowhere defined the var file

perhaps, it would be:

v_filename=${i##*/}  

ls -lrt | awk '{if($9 ~ /ABC/) print $9}'

In the above script I have some statements like

I am getting output like

Here the value of ${v_file_name} is missing. I want to create bad file and log file in the log directory while ctl file in the same directory test. I have used test/../log/ for this purpose but it is not working properly.

Please suggest me the possible cause.

Thanks
Vishalaksha

Have you this line in your script:

v_filename=${file##*/}

to:

v_filename=${i##*/}

as anchal_khare suggested?

I already corrected this... there were some errors at othere places also which have been corrected now. After all these corrections I have created a new file with same code and it is working fine now. There might a problem of some special character in previous file along with these errors.

Now I need one more suggestion from you all to achieve the below condition.

Can I put OR condition in above for loop?

Thanks in advance
Vishalaksha

Something like this:

for i in ${p_source_directory}/*.csv
do
  echo "the file is $i"
  case "$i" in
    *Nav*) (do your stuff here with *Nav* files) ;;
    *ABC*) (do your stuff here with *ABC* files) ;;
    *XY*)  (do your stuff here with *XY* files) ;;
  esac
done

Actually I am also using sql loader below in this for loop. It is possible that there are some files in the directory which may not belong to any of the customer. SO I want to prevent them appearing in the for loop.