Solved

How to parse a string in Perl

Posted on 2013-05-22
3
226 Views
Last Modified: 2013-05-23
How can I parse the following example string and get the expected results? This is just an example. The more general form is given below.

some/path/to/a/file.x:40: Error: (C 7-13): THIS will be removed in a future release. Use THAT without ERROR instead.

Open in new window


Expected output 1:
40

Open in new window


Expected output 2:
THIS will be removed in a future release. Use THAT without ERROR instead

Open in new window



The general form:

some/path/to/a/file.k:SOMENUMBER1: Error: (C SOMENUMBER2-SOMENUMBER3): SOME TEXT IN HERE

Open in new window


The expected output is:

SOMENUMBER1

Open in new window


SOME TEXT IN HERE

Open in new window

0
Comment
Question by:Tolgar
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39189286
$_='some/path/to/a/file.x:40: Error: (C 7-13): THIS will be removed in a future release. Use THAT without ERROR instead.';
@expectedoutput=(split/:/)[1,4];
0
 
LVL 7

Expert Comment

by:tampnic
ID: 39192095
Of course on a unix filesystem the colon ":" could potentially be part of a file name so tokenising the string with a "split" wouldn't work reliably. I'm a Perl beginner but I would be looking at using a regular expression to solve this. I know how I would write it in a sed script :-)

Cheers,
   Chris
0
 
LVL 84

Expert Comment

by:ozo
ID: 39192818
the colon ":" could potentially be part of a file name
the ":40:" or even the "THIS will be removed in a future release. Use THAT without ERROR instead" could potentially be part of a file name, so how would you reliably identify the SOMENUMBER1 and SOME TEXT IN HERE that you want?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl Sort Question 4 130
Having a problem with ActiveState Perl on 64-bit Windows 7 3 68
Using Perl to parse rows 7 93
Edit Odbc.ini using perl module 2 78
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now