perl regex string match issue..kindly help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl regex string match issue..kindly help
# 1  
Old 10-04-2012
perl regex string match issue..kindly help

i have a script in which i need to skip comments, and i am able to achieve it partially...
IN text file:

Code:
{****************************
{test : test...test }

Script:
Code:
while (<$fh>) 
	{
	  
	  push ( @data, $_);	
	  
	}
if ( $data[0] =~  m/(^{\*+$)/ ){
}

With the above match i am able to identify the comment and skip it to get the next line..
Out:
Code:
{test : test...test }

If the same comment consists of any space or any character in the place of *, my match fails..
IN:
{** ***** or {* abccd or {*abce
All the above cases are failed to skip ... What might be worng in the match,...can any one help me out..

Last edited by Scrutinizer; 10-04-2012 at 04:45 AM.. Reason: code tags
# 2  
Old 10-04-2012
I'm unfamiliar with the asterix as comment convention, however if you wish to skip from any occurrence of an asterix to the end of the line, you could capture the comment (or indeed ignore it, as follows:
Code:
~$ perl -e 'my $data="{*cheese"; print "$1\n" if $data=~/^{\*+([^*]+)?\**$/;'
cheese

# 3  
Old 10-04-2012
my match works fine if the input line has {****************************.....but there is a change in the input line as {* abccd or {*abce...it fails....
I tried to use the match
$data=~/^{\*+([^*]+)?\**$/;
but it throws error saying "Use of uninitialized value in concatenation (.) or string"
# 4  
Old 10-04-2012
From your inputs, it looks like all those lines are to be considered as a comment if it starts with {*, right?

If that is the case, you may simply use /^{\*/
# 5  
Old 10-04-2012
Quote:
Originally Posted by avskrm
my match works fine if the input line has {****************************.....but there is a change in the input line as {* abccd or {*abce...it fails....
I tried to use the match
$data=~/^{\*+([^*]+)?\**$/;
but it throws error saying "Use of uninitialized value in concatenation (.) or string"
That's a warning rather than an error (good policy to used warnings) if $1 isn't initialised (ie. there are no text elements in the comment) then printing it will throw that warning, you could check for text in the comment with an if($1){...} condition
# 6  
Old 10-04-2012
thanks....its working fine....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. Shell Programming and Scripting

Print lines that match regex on xth string

Hello, I need an awk command to print only the lines that match regex on xth field from file. For example if I use this command awk -F"|" ' $22 == "20130117090000.*" 'It wont work, I think, because single quotes wont allow the usage of the metacharacter star * . On the other hand I dont know... (2 Replies)
Discussion started by: black_fender
2 Replies

3. Shell Programming and Scripting

Perl:Regex for Search and Replace that has a flexible match

Hi, I'm trying to match the front and back of a sequence. It works when there is an exact match (obviously), but I need the regex to be more flexible. When we get strings of nucleotides sometimes their prefixes and suffixes aren't exact matches. Sometimes there will be an extra letter and... (2 Replies)
Discussion started by: jdilts
2 Replies

4. Shell Programming and Scripting

perl regex issue

Hi, I find it really strange while writing a simple regex to match and print the matched string, dibyajyo@fwtest:~ #perl -e '$x = "root@rashmi>"; print "matched string:$1\n" if ($x =~ /(root@rashmi)/);' matched string:root dibyajyo@fwtest:~ #perl -e '$x = "root@rashmi>"; print... (1 Reply)
Discussion started by: rrd1986
1 Replies

5. Shell Programming and Scripting

Perl newbie - regex replace all groups issue

Hello, Although I have found similar questions, I could not find advice that could help with my problem. The issue: I am trying to replace all occurrences of a regex, but I cannot make the regex groups work together. This is a simple input test file: The Vedanta Philosophy... (3 Replies)
Discussion started by: samask
3 Replies

6. Shell Programming and Scripting

Perl: Regex, string matching

Hi, I've a logfile which i need to parse and get the logs depending upon the user input. here, i'm providing an option to enter the string which can be matched with the log entries. e.g. one of the logfile entry reads like this - $str = " mpgw(BLUESOAPFramework):... (6 Replies)
Discussion started by: butterfly20
6 Replies

7. Shell Programming and Scripting

Issues with an exact match using regex in perl!

Hello Guys, I am trying to make an exact match for an email address entered as an argument, using perl, however, it's not working if I put a "$" in the email address. See the below outputs, Correct Match : bash-2.03$ echo sandy@test.com | perl -wln -e 'print if /(^*\@test.com$)/i'... (6 Replies)
Discussion started by: suffisandy
6 Replies

8. UNIX for Dummies Questions & Answers

Regex to match when input is not a certain string (can't use grep -v)

Hey everyone, Basically, all I'm looking for is a way to regex for not a certain string. The regex I'm looking to avoid matching is: D222 i.e. an equivalent of: awk '!/D222/' The problem is that I use this in the following command in a Bash script: ls ${source_directory} | awk... (1 Reply)
Discussion started by: kdelok
1 Replies

9. Shell Programming and Scripting

Perl REGEX - How do extract a string in a line?

Hi Guys, In the following line: cn=portal.090710.191533.428571000,cn=groups,dc=mp,dc=rj,dc=gov,dc=br I need to extract this string: portal.090710.191533.428571000 As you can see this string always will be bettween "cn=" and "," strings. Someone know one regular expression to... (4 Replies)
Discussion started by: maverick-ski
4 Replies

10. Shell Programming and Scripting

Perl Regex string opperation

I'm working on a basic log parser in perl. Input file looks like: len: 120713 foo bar file size of: testdir1/testdir1/testdir1/testdir1/testfile0 is 120713Of course there are tens of thousands of lines... I'm trying to compare the len and filesize values. #!/usr/bin/perl use strict; use... (2 Replies)
Discussion started by: dkozel
2 Replies
Login or Register to Ask a Question