How to compare file name with a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to compare file name with a string
# 1  
Old 03-15-2010
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
# 2  
Old 03-15-2010
Using perl you can compare the file name
Code:
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
Code:
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
Code:
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


Last edited by thillai_selvan; 03-15-2010 at 06:08 AM..
# 3  
Old 03-15-2010
Simply with case/esac :
Code:
#!/bin/sh

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

# 4  
Old 03-15-2010
Go to the directory /APPS/TEST/
Code:
 
ls -l | grep "ABC"

# 5  
Old 03-15-2010
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.

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

Thanks in advance
Vishalaksha
# 6  
Old 03-15-2010
Quote:
Originally Posted by vishalaksha
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
Code:
for file in /APPS/TEST/*
do
  case "$file" in
    *ABC*) 
       filename=${file##*/}
       echo "$filename"
       ;;
    *)
       ;;
  esac
done

# 7  
Old 03-15-2010
path=/home/user1/pictures/car.jpg
echo ${path##*/}
Now this will print only the file name
car.jpg
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. Shell Programming and Scripting

Compare user string with and predefined file...

i am trying to write shell script how to write shell script which is ask for user id and password then compare both from two different file.. i have no idea how to compare with data present in file.. #!/bin/bash -e read -p "User ID: " first read -p "Password: " second if ;then echo... (3 Replies)
Discussion started by: niko420
3 Replies

3. UNIX for Dummies Questions & Answers

Search for string in a file then compare it with excel files entry

All, i have a file text.log: cover6 cover3 cover2 cover4 other file is abc.log as : 0 0 1 0 Then I have a excel file result.xls that contains: Name Path Pass cover2 cover3 cover6 cover4 (1 Reply)
Discussion started by: Anamika08
1 Replies

4. Shell Programming and Scripting

Compare columns of multiple files and print those unique string from File1 in an output file.

Hi, I have multiple files that each contain one column of strings: File1: 123abc 456def 789ghi File2: 123abc 456def 891jkl File3: 234mno 123abc 456def In total I have 25 of these type of file. (5 Replies)
Discussion started by: owwow14
5 Replies

5. Shell Programming and Scripting

Compare two string in two separate file and delete some line of file

Hi all i want to write program with shell script that able compare two file content and if one of lines of file have # at the first of string or nothing find same string in one of two file . remove the line in second file that have not the string in first file. for example: file... (2 Replies)
Discussion started by: saleh67
2 Replies

6. Post Here to Contact Site Administrators and Moderators

Want a tcl script to compare a string in a file ignoring white spaces

Hi , I want a tcl script to search a string ignoring whitespaces in a .log file . It should correctly match . The string are as follows "Output-Maps 1 1 0 0 0" 1 and Active Intermediate-Maps 0 0 0 ... (1 Reply)
Discussion started by: kulua
1 Replies

7. UNIX for Dummies Questions & Answers

Compare 2 files print the lines of file 2 that contain a string from file 1

Hello I am a new unix user, and I have a work related task to compare 2 files and print all of the lines in file 2 that contain a string from file 1 Note: the fields are in different columns in the files. I suspect the is a good use for awk? Thanks for your time & help File 1 123 232 W343... (6 Replies)
Discussion started by: KevinRidley
6 Replies

8. Shell Programming and Scripting

How to compare particular string, if it is equal put into a new file

Hi, I have a file (sample.txt) this file contains below text. ./au ./11.5.0 ./11.5.0/admin ./11.5.0/admin/driver ./po ./11.5.0 ./11.5.0/admin ./11.5.0/admin/driver ./xxsbx/11.5.0/java/src ./xxsbx/11.5.0/lib ./xxsel ./xxsel/11.5.0 ./xxsel/11.5.0/admin ./zfa ./zfa/11.5.0... (2 Replies)
Discussion started by: gagan4599
2 Replies

9. Shell Programming and Scripting

String Compare

Hi i have a bash script which calls a PHP script (comparison_cron.php). The php script prints the string "no_data_to_retrieve" without quotes. Why does the following bash script not leave the while loop? #!/bin/bash cronOutput=$(php /srv/www/vhosts/xyz/httpdocs/comparison_cron.php)... (2 Replies)
Discussion started by: kaphiphi
2 Replies

10. Shell Programming and Scripting

Compare String

Hi everybody. I have this comand "/usr/local/check_procs myprocess" which prints "OK: myprocess running" on my console if the process is running and "myprocess not running" if it is not. I am writing a bash script to check if the process is running but am getting an error. Here is my... (3 Replies)
Discussion started by: kanexxx
3 Replies
Login or Register to Ask a Question