LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   simple parsing question (https://www.linuxquestions.org/questions/programming-9/simple-parsing-question-409782/)

thanhvn 01-30-2006 07:06 PM

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,

xhi 01-30-2006 08:48 PM

can you post a sample of the lines that will need parsed..

thanhvn 01-30-2006 11:18 PM

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.

kshkid 01-31-2006 01:25 AM

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.

xhi 01-31-2006 08:05 AM

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";



All times are GMT -5. The time now is 05:04 PM.