PERL : pattern matching a string stored in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL : pattern matching a string stored in a variable
# 1  
Old 11-09-2011
PERL : pattern matching a string stored in a variable

I have two variables,

Code:
 
my $filename = "abc_yyyy_mm_dd.txt";
my $filename1 = " abc_2011_11_07.txt";

I need to perform some operations after checking if $filename has $filename1 in it

i have used the below code,

Code:
 
if($filename =~ /^$filename1/)
{
----
--
}

but this doesnt work.

Can somebody help with this.
Thanks in advance.
# 2  
Old 11-09-2011
if you want to compare the strings this may help you..

Quote:
#!/usr/bin/perl
$filename = "abc_yyyy_mm_dd.txt";
$filename1 = "abc_2011_11_07.txt";
chomp $filename;
chomp $filename1;

if( $filename eq $filename1 )
{
print "same";

}
else
{
print "different"
}

# 3  
Old 11-09-2011
Code:
my $filename = "abc_yyyy_mm_dd.txt"; my $filename1 = " abc_2011_11_07.txt";

Quote:
Originally Posted by irudayaraj
checking if $filename has $filename1 in it
But $filename does NOT have $filename1 in it.
I suggest you play with the =~ operator a little bit.

Code:
my $filename = "abc_yyyy_mm_dd.txt"; my $str = "yy_m";
if ($filename =~ $str) {
  print "Yess!\n"
}

will print Yess! because $filename contains "yy_m".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep a sub-string from a string stored in a variable.

For example: I am grepping "Hello" from a file and there are 10 matches. So all ten lines with match will get stored into a variable($match). Now I want to ignore those lines which have "Hi" present in that. Currently I tried this: match = grep "Hello" file | grep -v "Hi" file But that's not... (2 Replies)
Discussion started by: pavan
2 Replies

2. Shell Programming and Scripting

awk to lookup stored variable in file and print matching line

The bash bash below extracts the oldest folder from a directory and stores it in filename That result will match a line in bold in input. In the matching line there is an_xxx digit in italics that (once the leading zero is removed) will match a line in link. That is the lint to print in output.... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

4. 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

5. Shell Programming and Scripting

Pattern Matching in PERL

I have a 2 files in .gz format and it consists of 5 million lines the format of the file would be gzcat file1.gz | more abcde aerere ffgh56 .. .. 12345 gzcat file2.gz | more abcde , 12345 , 67890, ffgh56 , 45623 ,12334 whatever the string is in the file1 should be matched... (3 Replies)
Discussion started by: aravindj80
3 Replies

6. Shell Programming and Scripting

Delete first character from a string stored in a variable

Hallo! Example. #!/bin/bash BACKUP_DIR=/home/userx/backups/evolution echo $BACKUP_DIR # delete the first character from the string BACKUP_DIR=$(echo $BACKUP_DIR | cut -c 2-) echo $BACKUP_DIR It works. It does want I want, delete the first character from string in the... (11 Replies)
Discussion started by: linuxinho
11 Replies

7. Shell Programming and Scripting

AWK: pattern not properly stored in variable?

Hey there, I have a table of contents file of the form 1 Title1 1.1 Subtitle1 1.1.1 Subsubtitle1 1.1.2 Subsubtitle2 ... and want to count the number of dots in the first field to find out the level of the section. I use the gsub function for the job, which works if I pass the pattern... (2 Replies)
Discussion started by: herrsimon
2 Replies

8. Shell Programming and Scripting

how to get the string stored in a variable in a line???

Hi all, I want to search for a data type in a line.For this in a loop i am checking for $DATA_TYPE in a line using grep.But grep is not able to find when i give this. Can any one tell me how to check string in $DATA_TYPE variable in line usign grep (or) any other way to do the above task. ... (4 Replies)
Discussion started by: jisha
4 Replies

9. Shell Programming and Scripting

perl pattern matching

hi i am trying to get digits inside brackes from file , whose structure is defined below CREATE TABLE TELM (SOC_NO CHAR (3) NOT NULL, TXN_AMOUNT NUMBER (17,3) SIGN_ON_TIME CHAR (8) TELLER_APP_LIMIT NUMBER (17,3) FIL01 ... (2 Replies)
Discussion started by: zedex
2 Replies

10. Shell Programming and Scripting

Perl: Pattern Matching a HASH variable

Hi All, I'm trying to test a Hash variable but it's not working. Here is my code - can anyone tell me if the test is valid? for (keys %enabled_yn) { if ($enabled_yn{$1} =~ m/\s+Y/) { $html =~ s/%(\w+)%/\<b\>\<font color\=orange\>$enabled_yn{$1}\<\/font\>\<\/b\>/g; }... (1 Reply)
Discussion started by: pondlife
1 Replies
Login or Register to Ask a Question