Hi,
Below are the 2 files in directory /tmp:
masterCSF242323.img
indexCSF242323.img
1) I want to compare if both the number (242323) are same in both the files. If they are same print - Files matching, else print files do not match.
2) Also if only index file is present in that directory, I want to print- only index file is present.
Please help me in this using regular expression or anything else.
Thanks in Advance.
Try this
#! /bin/bash
file1=`ls /tmp/masterCSF* | head -1`
file2=`ls /tmp/indexCSF* | head -1`
if [ -z "$file1" ]; then
echo " Only indexCSF file is available"
exit 1
fi
val1=`echo "$file1" | tr -dc [0-9]`
val2=`echo "$file2" | tr -dc [0-9]`
if [ "$val1" = "$val2" ];then
echo "File match"
else
echo "File does not match"
fi
1 Like
Please try this:
let MSTNUM=$(echo masterCSF242323.img | sed -e 's/[^0-9]*//g')
let IDNUM=$(echo indexCSF242323.img | sed -e 's/[^0-9]*//g')
if [[ ${MSTNUM} -eq ${IDNUM} ]]; then
echo "The numbers are equal"
else
echo "They are not"
fi
You can also use Shell Builtin
MST=masterCSF242323.img
MSTNUM=${MST%.img}; MSTNUM=${MSTNUM#masterCSF}
IDT=indexCSF242323.img
IDNUM=${IDT%.img}; IDNUM=${IDNUM#indexCSF}
if [[ ${MSTNUM} -eq ${IDNUM} ]]; then
echo "The numbers are equal"
else
echo "They are not"
fi
1 Like
Thanks a lot....
Could any one help me in perl?
If you're working in UNIX, you're going to need to learn the shell... I wrote some horrible perl scripts before I realized 99% of what I was doing was shell work. Any particular reason the shell ones won't work?
Yes, All the scripts of mine are perl.
I have something like this here,
m/^index(CSF|TAS)([0-9a-zA-Z]+)\.([a-zA-Z][a-zA-Z][a-zA-Z])$/i
But dont know how to implement further.
Any particular reason? I wrote some really horrible perl scripts before I realized 99% of what I was doing was actually shell work, much simpler and more efficient in that language.
$ wc -l perl-backup.pl bash-backup.sh
49 perl-backup.pl
25 bash-backup.sh
74 total
$
Anyway. That regex looks a lot more complicated than you need. Why match all the stuff you don't care about? Just match the things you want, and enough to know it's where it belongs, at the end.
#!/usr/bin/perl
my $FILE1="masterCSF242323.img";
my $FILE2="indexCSF242323.img";
$FILE1 =~ /([0-9]+)\.[^0-9]*$/;
$NUM1=$1;
$FILE2 =~ /([0-9]+)\.[^0-9]*$/;
$NUM2=$1;
No. What if there are other files other than the above starting with Index and master and ending with any other number? Thats why I used
m/^index(CSF|TAS)([0-9a-zA-Z]+)\.([a-zA-Z][a-zA-Z][a-zA-Z])$/i
find my below code, but not working fine:
#!/usr/bin/perl
my $directory = '/devl/pcw/ndm/statement/csf_retail/image/';
opendir (DIR, $directory) or die $!;
while (my $file = readdir(DIR)) {
if ( $file =~ m/^index(CSF|TAS)([0-9a-zA-Z]+)\.([a-zA-Z][a-zA-Z][a-zA-Z])$/i )
{
print "Extractor Parser: index file found: $file, master file to search for is: master$1$2.$3\n";
if ( -e "master$1$2.$3" ) {
print "File Exists!\n";
}
else
{
print "File not matched\n";
}
}
}
Here the files I have used is : indexCSF242323.img masterCSF261799.img
Ideally it should print, files not matched. But it not printing.
Please help!
thanks.