LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-30-2006, 07:06 PM   #1
thanhvn
Member
 
Registered: Mar 2005
Location: CA
Distribution: RHEL3, FC4
Posts: 46

Rep: Reputation: 15
simple parsing question


I have a string as follows:

Errors xx, Warnings yy (zz)

where xx, yy, and zz are numbers; everything else are literals.

How can I parse this string to extract the xx, yy, and zz numbers? awk, sed, perl, python, etc. are fair game as long as I can easily use it in a shell script.

Thanks,
 
Old 01-30-2006, 08:48 PM   #2
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
can you post a sample of the lines that will need parsed..
 
Old 01-30-2006, 11:18 PM   #3
thanhvn
Member
 
Registered: Mar 2005
Location: CA
Distribution: RHEL3, FC4
Posts: 46

Original Poster
Rep: Reputation: 15
Hmm, perhaps I wasn't being perfectly clear, but here it goes. Suppose I have a bunch of the following lines:

Errors 1, Warnings 2 (3)
Errors 4, Warnings 5 (6)
Errors 7, Warnings 8 (9)
Errors 10, Warnings 11 (12)
Etc...

I want to extract the individual numbers in the above lines...so that I can sum them, for example:

Total errors: 22
Total warnings: 26 (30)

I hope that is clear enough. What I wanted is a fairly efficient (but not overly complicated, understandability/maintainability is important too) way to parse the above example lines to put in my shell script. Awk, sed, perl, python, etc. invocations are acceptable as long as these tools are fairly well known and come standard (i.e. preinstalled) on most Linux distros.
 
Old 01-31-2006, 01:25 AM   #4
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
try this,

Code:
>echo "Errors 10, Warnings 11 (12)" | sed 's/\(Errors \)\(.*\), \(Warnings\) \(.*\) (\(.*\))/\2 \4 \5/'
>10 11 12
will have the numbers seperated then read them into a variable, process the file in a loop and sum all the numbers.

Hope this helps.
 
Old 01-31-2006, 08:05 AM   #5
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by thanhvn
Hmm, perhaps I wasn't being perfectly clear, but here it goes. Suppose I have a bunch of the following lines:

Errors 1, Warnings 2 (3)
Errors 4, Warnings 5 (6)
Errors 7, Warnings 8 (9)
Errors 10, Warnings 11 (12)
Etc...

I want to extract the individual numbers in the above lines...so that I can sum them, for example:

Total errors: 22
Total warnings: 26 (30)

I hope that is clear enough. What I wanted is a fairly efficient (but not overly complicated, understandability/maintainability is important too) way to parse the above example lines to put in my shell script. Awk, sed, perl, python, etc. invocations are acceptable as long as these tools are fairly well known and come standard (i.e. preinstalled) on most Linux distros.
Hmm, perhaps you were. I didnt read the first example to be literally the string..
here is somthing that should work in perl
Code:
$line = "Errors 5, Warnings 4 (3)";
$line =~ /^.*?(\d+).*?(\d+).*?(\d+)/;
print "$1 $2 $3";
or simply
Code:
$line = "Errors 5, Warnings 4 (3)";
$line =~ /^Errors (\d+), Warnings (\d+) \((\d+)/;
print "$1 $2 $3";
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ubuntu Fluxbox simple question, simple answer? generallimptoes Linux - Software 3 09-26-2005 02:03 PM
Parsing of a simple expression. KissDaFeetOfSean Programming 1 07-18-2005 04:45 PM
sed parsing question ncblues Linux - Newbie 5 01-03-2005 06:36 AM
Text parsing question bruoersolitario Linux - General 4 04-15-2004 02:12 PM
Installing Programs - A simple question from my simple mind jmp875 Linux - Newbie 6 02-18-2004 09:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration