Perl :: to parse the data from a string.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl :: to parse the data from a string.
# 1  
Old 07-17-2013
Perl :: to parse the data from a string.

Hi folks,

I have a line in log from which I need to parse few data.
Code:
Jul  6 00:05:58 dg01aipagnfe01p %FWSM-3-106011: Deny inbound (No xlate)

From the above... I need to parse the %FWSM-3-106011: substring.

Another example
Code:
Jul 13 00:08:55 dq01aipaynas01p %FWSM-6-302010: 2 in use, 1661 most used

from the above Ineed to parse the %FWSM-6-302010: substring in perl.

Could anyone please let me know how to parse as mentioned above in PERL..


Thanks in advance ....

Regards,
J
# 2  
Old 07-17-2013
Quote:
Originally Posted by scriptscript
...
I have a line in log from which I need to parse few data.
Code:
Jul  6 00:05:58 dg01aipagnfe01p %FWSM-3-106011: Deny inbound (No xlate)

From the above... I need to parse the %FWSM-3-106011: substring.

Another example
Code:
Jul 13 00:08:55 dq01aipaynas01p %FWSM-6-302010: 2 in use, 1661 most used

from the above Ineed to parse the %FWSM-6-302010: substring in perl.

Could anyone please let me know how to parse as mentioned above in PERL..
...
Please post the attempts you've made so far to solve this problem.
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 07-17-2013
Hi durden_tyler,


Code:
$line = "Jul 13 00:08:55 dq01aipaynas01p %FWSM-6-302010: 2 in use, 1661: most used";

$line =~ m/(\%(.*)\:)/;

print "\n matched";
print $&;

## printing %FWSM-6-302010: 2 in use, 1661:  but expected is %FWSM-6-302010:

Could you please help me ???

Last edited by Corona688; 07-17-2013 at 07:04 PM..
# 4  
Old 07-17-2013
Quote:
Originally Posted by scriptscript
...
Code:
$line = "Jul 13 00:08:55 dq01aipaynas01p %FWSM-6-302010: 2 in use, 1661: most used";
$line =~ m/(\%(.*)\:)/;
...
## printing %FWSM-6-302010: 2 in use, 1661:  but expected is %FWSM-6-302010:

...
A few thoughts on your regex:

(1)
Code:
.*

is a "greedy quantifier". It will match as much of the string as possible while still allowing the entire regex to match the string.

So the match starts from the "%" character and it matches as many characters as possible to reach the next ":" character. That is the reason the match goes beyond the first ":" character, and goes on till the second (and last) ":" character.

What you are looking for is a "non-greedy quantifier", which is formed by the usual quantifier appended by a "?". That will match as few characters as possible and stop at the first ":" after the "%" character.

Search for "non-greedy quantifier" in the official Perl regex tutorial : perlretut - perldoc.perl.org for a detailed review of this concept.

(2)
You don't need any of the braces
Code:
( )

unless you want to store the matched string in special variable
Code:
$1

and the part of the string between "%" and ":" in special variable
Code:
$2

(3)
"%" and ":" are not special characters, so you don't have to escape them. To see a legitimate list of special characters, search for "metacharacters" in the Perl regex tutorial: perlretut - perldoc.perl.org
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Need command to parse data

Hi Friends, I have data like below t064266 I want output into this format t064266 Data are space delimited and i want parse third column data. Thanks (9 Replies)
Discussion started by: Jagaat
9 Replies

2. Programming

Perl parse string

Hi Perl Guys I have another perl question I have the following code that i have written Getopt::Long::config(qw( permute bundling )); my $OPT = {}; GetOptions($OPT, qw( ver=s help|h )) or die "options parsing failed"; This will allow the user to do something like... (4 Replies)
Discussion started by: ab52
4 Replies

3. Shell Programming and Scripting

Regex to Parse data

Experts and Informed folks, Need some help here in parsing the log file. 1389675 Opera_ShirtCatalog INSERT INTO Opera_ShirtCatalog(COL1, COL2) VALUES (1, 'TEST1'), (2,'TEST2'); 1389685 Opera_ShirtCatlog_Wom INSERT INTO Opera_ShirtCatlog_Wom(col1, col2, col3) VALUES (9,'Siz12, FormFit',... (12 Replies)
Discussion started by: ManoharMa
12 Replies

4. Shell Programming and Scripting

Parse data

Guys , please help me out with another AWK solution ... Input Device Physical Name : Not Visible Device Symmetrix Name : 0743 Front Director Paths (2): { ---------------------------------------------------------------------- ... (5 Replies)
Discussion started by: greycells
5 Replies

5. Shell Programming and Scripting

parse a mixed alphanumeric string from within a string

Hi, I would like to be able to parse out a substring matching a basic pattern, which is a character followed by 3 or 4 digits (for example S1234 out of a larger string). The main string would just be a filename, like Thisis__the FileName_S1234_ToParse.txt. The filename isn't fixed, but the... (2 Replies)
Discussion started by: keaneMB
2 Replies

6. Shell Programming and Scripting

perl search string for cut data

perl -lne '$/="1H1XXXXX";print $_ if /0001|0002|0003/' data.txt> output.txt more data.txt 1H1XXXXX|0001|Y| aaa bbb ccc 1H1XXXXX|0005|N| bbb g 1H1XXXXX|0001|Y| hhh ddd 222 1H1XXXXX|0002|Y| 444 1H1XXXXX|0002|N| 222 1H1XXXXX|0003|Y| hhhh (3 Replies)
Discussion started by: kittiwas
3 Replies

7. Shell Programming and Scripting

Parse data

hi i have a file p1.htm <div class="colorID2"> aaaa aaaa aa <br/> bbbbbbbb bbb<br/> <br/>cccc ccc ccc </div><div class="colorID1"> dddd d ddddd<br/> eeee eeee eeeeeeeeee<br/> fffff <br/>g gg<br/> (5 Replies)
Discussion started by: saw7
5 Replies

8. Shell Programming and Scripting

How to parse data?

Hi all, I have output of paction command looking like this: RELCI 0 IP address 1.2.16.3 Xmit: CURRENT Recv: WAIT_HEADER 0 congestions 2617/0 buf. sent/rec Xmit: CURRENT Recv: WAIT_HEADER 0 congestions 0/0 buf. sent/rec BUFFER Xmit: ... (6 Replies)
Discussion started by: sameucho
6 Replies

9. Shell Programming and Scripting

Perl parse string to time

Hi, I have got this value 18:21:23.330 in one of my variables. Now I need to parse this time to something. And then I have to compare it with 2 times, let's say, 15:00 hrs to 23:00 hrs. Can Date::Manip rescue me from this horrifying situation? I am quite new to Perl and especially this... (1 Reply)
Discussion started by: King Nothing
1 Replies

10. Shell Programming and Scripting

Perl: Search for string then parse next line

Hi All, I have a file that I need to be able to find a pattern match on one line then parse data on the next or subsequent lines - I will know which line needs to be parsed beforehand. This is what I currently have: while (<COMMAND_OUT>) { if ($_ =~ m/TEST/) { ... (4 Replies)
Discussion started by: pondlife
4 Replies
Login or Register to Ask a Question