0

You might say that this is a duplicate post ... but I have tried previous solutions and they do not seem to work! For perl for some reason I see solutions involving substitution replacement but they replace the text matching pattern instead of displaying it!. Below is the source text

ClassOne:error=9607
ClassTwo:This is junk test
ClassThree:I|want|to|extract|this|text|after|error=9607

I want to extract all lines containing ClassThree:.... after I have found error=9607. Preferably I want a single line command to accomplish this because I will run it across multiple files on large number of servers (using command in line with ssh command).

Help/pointers in this regard is appreciated.

1
  • Thanks all for solutions! Unfortunately none of the below commands worked on my servers which I think would be because of old version of AIX which runs on my server. Luckily I looked more into awk based on @potong's comments and that seemed to have the solution awk /begin/,/end/ filename pretty neat eh? :-)
    – jeet
    Feb 23, 2012 at 18:36

4 Answers 4

1

If ClassThree will always be 2 lines after the error=9607, you can just:

grep -A 2 'error=9607' input.txt
1

This might work for you:

sed '/error=9607/,/ClassThree/{//!d}' file
ClassOne:error=9607
ClassThree:I|want|to|extract|this|text|after|error=9607

If you only want the ClassThree line:

sed '/error=9607/,/ClassThree/{/ClassThree/!d}' file
ClassThree:I|want|to|extract|this|text|after|error=9607

If you want all lines containing ClassThree thereafter:

cat <<! >file
> ClassOne:error=9607
> ClassTwo:This is junk test
> ClassThree:I|want|to|extract|this|text|after|error=9607
> ClassOne:error=9608
> ClassTwo:This is junk test
> ClassThree:I|want|to|extract|this|text|after|error=9607
> ClassOne:error=9609
> ClassTwo:This is junk test
> ClassThree:I|want|to|extract|this|text|after|error=9607
>!
sed '/error=9607/h;G;/ClassThree.*\n./P;d' file
ClassThree:I|want|to|extract|this|text|after|error=9607
ClassThree:I|want|to|extract|this|text|after|error=9607
ClassThree:I|want|to|extract|this|text|after|error=9607

or this awk solution:

 awk '/error=9607/{p=1};/ClassThree/ && p' file
0

Here is a perl attempt

Edit: I added specific error checking as well

use strict;
my @errors = (9607,9608);
open(FILE,"filescan.txt")||die "Error: cannot read file\n$!";
while(<FILE>){
    chomp(my $line = $_ );
    if ( $line =~ /^ClassOne:error=(\d+)/ ) {
        my $errorCode = $1;
        if ( grep { $_ == $errorCode } @errors ) {
            print "$line\n";
            while(<FILE>){
                chomp(my $line = $_ );
                if ( $line =~ /^ClassThree:.*\|error=$errorCode/ ) {
                    print "$line\n";
                    last;
                }
            }
            print "----------\n";
        }
    }
}
close FILE;

Output:

ClassOne:error=9607
ClassThree:I|want|to|extract|this|text|after|error=9607
----------
ClassOne:error=9608
ClassThree:I|want|to|extract|this|text|after|error=9608
----------
0
$ perl -ple 'print if /^ClassThree/ && /error=9607/ .. /\0/' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.