tags:

views:

108

answers:

6

Hi , I have a file with the following entries

--INFO----- Command processing: Name='shayam' Age='19' Project='Alwa'    
--ERROR---- Failed to process  
--INFO----- Command processing: Name='ram' Age='23' Project='Alwa'  
--INFO----- Command processing: Name='raja' Age='24' Project='Alwa'  
--INFO----- Command processing: Name='shyla' Age='27' Project='Alwa'  
--ERROR---- Failed to process

I need to extract Name and Age from the corresponding line for which error occurred. In this case, I need to extract Name=shayam, Age=19 and Name=shyla Age=27.

+1  A: 

I would start with:

$ echo "--INFO----- Command processing: Name='shayam' Age='19' Project='Alwa'
--ERROR---- Failed to process
--INFO----- Command processing: Name='ram' Age='23' Project='Alwa'
--INFO----- Command processing: Name='raja' Age='24' Project='Alwa'
--INFO----- Command processing: Name='shyla' Age='27' Project='Alwa'
--ERROR---- Failed to process " | perl -ne '
    if (/^--INFO--/) {@line = split;}
    if (/^--ERROR--/) {print "$line[3] $line[4]\n";}'

which produces:

Name='shayam' Age='19'
Name='shyla' Age='27'

All it does is store the information from every INFO line and then print it out when you get an ERROR line.

You'll notice it still has the quotes around the values but, if you really want to get rid of those, use the (very simplistic) proc.pl script:

#!/bin/perl -w
while (<STDIN>) {
    if (/^--INFO--/) {
        @line = split;
    }
    if (/^--ERROR--/) {
        $l3 = $line[3];
        $l4 = $line[4];
        $l3 =~ s/'//g;
        $l4 =~ s/'//g;
        print "$l3 $l4\n";
    }
}

Running this with:

$ echo "--INFO----- Command processing: Name='shayam' Age='19' Project='Alwa'
--ERROR---- Failed to process
--INFO----- Command processing: Name='ram' Age='23' Project='Alwa'
--INFO----- Command processing: Name='raja' Age='24' Project='Alwa'
--INFO----- Command processing: Name='shyla' Age='27' Project='Alwa'
--ERROR---- Failed to process " | ./proc.pl

gives:

Name=shayam Age=19
Name=shyla Age=27

You can use any input file or stream with that (for example):

cat file.txt | ./proc.pl

or:

./proc.pl <file.txt
paxdiablo
how to include in perl script? I have the entries populated in one file.
shayam
+1  A: 

I'm not sure if I understand the title of this question. If you're programming using Perl, you could use a regex to capture the information like so:

/Name='(.*?)' Age='(.*?)'/

The name will be in $1 and the age will be in $2.

Benjamin Oakes
But it will extract all the Name and Age field. I need to extract only the previous line Name and Age field where the ERROR occured..
shayam
A: 

Try with:

sed -n "\$!N;/--ERROR----/{s/.*Name='\([^']*\)' Age='\([^']*\)'.*/Name=\1 Age=\2/p}" file

If you are using bash, you may have to disable history substitution:

set +H
marco
+1 Now that's compact.
Mike
+2  A: 

I would stick to the same approch of the other answer to the similar question, simply storing the last INFO found so far

$p = "";
while(<>) {
    if ( /^--INFO/ ) { $p = $_; next; }
    next if !/^--ERROR/;
    $p =~ /Name='([^']+)'\s+Age='([^']+)'/;
    print $1, "  ", $2, "\n";
}
ShinTakezou
Hi shin, It is not working. it goes for infinite loop.. :(
shayam
Hi shin, its working.. sorry made one small mistake.. you are really kewl.. thanks a lot.. Need to appreciate your help..
shayam
Hi shin if the single quotes is not there in the Name feild, then its not working..
shayam
of course. the regex is built upon your sample output. you have to change it according to what you really have; e.g. in the previous sample (other question), you used ". If you can use both indifferently you can change ' with ['"] even though this is simplistic since allow for `'Name"` which usually is not correct
ShinTakezou
+2  A: 

This is similar to the other answers here, just simplified a little:

#!/bin/perl -w
use strict;
my ($name, $age);
while(<>) {
    ($name, $age) = ($1, $2) if /Name='?([\w]+)'?\s+Age='?([\d]+)'?/;
    print "$name, $age\n" if /^--ERROR--/;
}

Usage example:

perl extract.pl input.log
Mike
thanks mike.. this solution too worked.. you people in stactoverflow are really genius.
shayam
Hi Mike, if the single quotes is not there in the Name field, then its not working. How to modify the code?
shayam
But it is not working dude.. tats why posted..
shayam
@shayam: I've updated the code so that it should work with or without single quotes in the input file.
Mike
A: 
Greg Bacon