Operating System - HP-UX
1755302 Members
3683 Online
108831 Solutions
New Discussion юеВ

Cut part of txt file with specified terms....

 
SOLVED
Go to solution
LITWINCZUK
Advisor

Cut part of txt file with specified terms....

Normal procedure is started with: (A)
00000000.3987 2010-04-13 09:04:22 argv[0]: './encrypt'
00000000.3987 2010-04-13 09:04:22 argv[1]: './'

And end with: (B)
00000014.9556 2010-04-13 09:00:21 SendMessage: wiadomosc wyslana
00000015.1078 2010-04-13 09:00:22 wiadomosc wyslana
00000015.1078 2010-04-13 09:00:22 ~Message()


But sometime i have error (C):
00000006.3951 2010-04-13 09:04:28 Encrypt ret: 7
00000006.3951 2010-04-13 09:04:28 blad szyfrowania
00000006.3951 2010-04-13 09:04:28 ~Message()

Normal pass is: (A) ..... (B) etc....

Error: (A) ..... (C) (no B here !)

I need to get:
1. All errors from file - section A -> C (there are no B in this pass), but without any normal, sucessfull termination (section A->B)
2. Only last error pass.

Can i do this with sed command?
To cut all errors, only one A-C per error ?
10 REPLIES 10
LITWINCZUK
Advisor

Re: Cut part of txt file with specified terms....

and one again:
A - unique in this section is
argv[0]: './encrypt'

C - unique text is
Encrypt ret: 7
Dennis Handly
Acclaimed Contributor

Re: Cut part of txt file with specified terms....

>Can I do this with sed command?

This seems much too complex for sed. awk may be better.

>To cut all errors, only one A-C per error?

You are going to have to provide much more details, actual files. I'm not sure what is A, B or C.

Perhaps this is easier, what do you not want?
LITWINCZUK
Advisor

Re: Cut part of txt file with specified terms....

There are:

1. normall pass:
start with section A, next some other lines... and end with B.

2. Pass with error: starti with A, some other lines... end with C.


Of course, as normal program function there are many A-B in log and only few with A-C.

I need to cut only sections with A-C and throw away all A-B.

When i trying something like
sed -n -e '1,/argv\[0\]: /p' -e '1,/Encrypt ret: 7/p' plik.txt

i get start with A.... (first from start of file) to first error - C section. But with all A-B in this file. I must remove them.
Best is cut on first C section and go backwards line by line untill i get first A section.
But i dont know to do it :/

Dennis Handly
Acclaimed Contributor

Re: Cut part of txt file with specified terms....

How many lines are in A, B and C, exactly like you have except for the leading dates/time?

What varies?

>Best is cut on first C section and go backwards line by line until I get first A section.

Backwards doesn't work well unless you have arrays, with awk or perl.

Do you have some example data?
LITWINCZUK
Advisor

Re: Cut part of txt file with specified terms....

>How many lines are in A, B and C, exactly like you have except for the leading dates/time?

This is different. Dont look at date.
Sections like "00000000.3987 2010-04-13 09:04:22" is always different. Number of lines between beginning and end is always different.

I want to focus on:
argv[0]: './encrypt'
Encrypt ret: 7
SendMessage: wiadomosc wyslana

Upper words is unique in logs. If i try to catch all lines between it, it will work fine.


>Do you have some example data?
There are some data, also uuencoded area. This is log from delta encryptor - so i cannot post all of them.
James R. Ferguson
Acclaimed Contributor

Re: Cut part of txt file with specified terms....

Hi:

Dennis asked you "How many lines are in A, B and C, exactly like you have except for the leading dates/time?"

You haven't answered this question. Are there always some set number of lines to A, B and/or C? Is there some specific delimiter between A, B, and C as for example a blank line?

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Cut part of txt file with specified terms....

Hi (again):

You might try playing and adapting something like this:

# cat ./extract
#!/usr/bin/perl
use strict;
use warnings;
my $AVAL='a'; #...change to what you want to match...
my $BVAL='b'; #...ditto
my $CVAL='c'; #...ditto
my ( @list, $rangeop );
sub spout {
#...the list must contain $AVAL and $CVAL to be a candidate...
@list = grep( !/$BVAL/, @list );
if ( grep( /$CVAL/, @list ) ) {
print ">>> $_\n" for @list;
}
@list=();
}
while (<>) {
chomp;
if ( $rangeop = /$AVAL/../$CVAL/ ) {
@list = () if /$AVAL/;
push @list, $_;
}
if ($rangeop =~ m/E0/) { #...signifies range's end...
spout();
}
}
END {
spout();
}
1;

...which if run interactively (where lines with ">>> " are the output generated at that point in the line-by-line input) would look like:

# ./extract
a
b
a
b
c
>>> a
>>> c
aaa
xxx
yyy
zzz
bbb
bbb
ccc
>>> aaa
>>> xxx
>>> yyy
>>> zzz
>>> ccc
a
c
>>> a
>>> c
a
b
a
a
a
b
b
c
>>> a
>>> c

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor
Solution

Re: Cut part of txt file with specified terms....

Assuming you start with "argv[0]:" and end with "Encrypt ret: 7" and 2 more lines or "SendMessage: wiadomosc wyslana" and 2 more lines you could use this awk script:
awk '
BEGIN { count=0; collect=0 }
function print_lines() {
for (i = 1; i <= count; ++i)
print save[i]
count = 0;
}
function save_line() {
save[++count] = $0
}
/argv\[0]:/ { # A start
collect = 1
save_line()
next
}
/Encrypt ret: 7/ { # C error
save_line() # save 3
getline; save_line()
getline; save_line()
print_lines()
collect = 0
next
}
/SendMessage: wiadomosc wyslana/ { # B normal
getline; getline # flush 3
count = 0
collect = 0
next
}
collect {
save_line()
next
}' input-file
LITWINCZUK
Advisor

Re: Cut part of txt file with specified terms....

THANKS!