Home > Archive > AWK > August 2007 > delete line preceeding foobar
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
delete line preceeding foobar
|
|
| di98mase 2007-08-01, 3:57 am |
| Hi,
I have browsed the forum for input to this issue but without any
simple answer.
This is an extract of my file I want to process.(line numbers are not
part of original file)
1 63.259 tsmc_cs: ALG->Supersys/R/0x Data (31/31)
2 ae001c006446adb0db00
3
4 63.260 ltu: ALG->LTU/R/0x Data (6/6)
5 a40003006700
6
7 63.377 ltu: ALG->LTU/R/0x Data (5/5)
8 0000020000
9
10 63.378 ltu: Keep_Rsp->ALG/S/0x Data (5/5)
11 0000020001
Now what I want to do is ro remove the lines 7 and 8. What I tried was
this:
match($0, /lctu: ALG->LTU/, /^0000020000/);
if (RSTART != 0)
{
getline;
getline;
next;
}
but this "cleans" the entire file???
How should I do?
| |
|
| On 1 Aug., 09:26, di98mase <di98m...@hotmail.com> wrote:
> Hi,
>
> I have browsed the forum for input to this issue but without any
> simple answer.
>
> This is an extract of my file I want to process.(line numbers are not
> part of original file)
>
> 1 63.259 tsmc_cs: ALG->Supersys/R/0x Data (31/31)
> 2 ae001c006446adb0db00
> 3
> 4 63.260 ltu: ALG->LTU/R/0x Data (6/6)
> 5 a40003006700
> 6
> 7 63.377 ltu: ALG->LTU/R/0x Data (5/5)
> 8 0000020000
> 9
> 10 63.378 ltu: Keep_Rsp->ALG/S/0x Data (5/5)
> 11 0000020001
>
> Now what I want to do is ro remove the lines 7 and 8. What I tried was
> this:
> match($0, /lctu: ALG->LTU/, /^0000020000/);
> if (RSTART != 0)
> {
> getline;
> getline;
> next;
> }
>
> but this "cleans" the entire file???
>
> How should I do?
Your match() pattern does not match your input data above. And your
subject line does not seem to match to the contents of your posting.
To remove all blocks that contain a certain pattern...
BEGIN{RS="";ORS="\n\n"}!/pattern/
to keep all blocks that contain a certain pattern...
BEGIN{RS="";ORS="\n\n"}/pattern/
Janis
| |
| di98mase 2007-08-01, 7:57 am |
| On 1 Aug, 10:47, Janis <janis_papanag...@hotmail.com> wrote:
> On 1 Aug., 09:26, di98mase <di98m...@hotmail.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
> Your match() pattern does not match your input data above. And your
> subject line does not seem to match to the contents of your posting.
>
> To remove all blocks that contain a certain pattern...
>
> BEGIN{RS=3D"";ORS=3D"\n\n"}!/pattern/
>
> to keep all blocks that contain a certain pattern...
>
> BEGIN{RS=3D"";ORS=3D"\n\n"}/pattern/
>
> Janis- D=F6lj citerad text -
>
> - Visa citerad text -
You are absolutely right, I am sorry for my unclear post. Let me try
to clarify what I mean.
What I want to do is to remove the line above each line begining with
the pattern "0000020000", I dont know how the line preceding might
look, thus I can't search for anything in line 8. Correct about the
pattern unmatch in my script the pattern is ltu in both the pattern
and the file. (I did some editing in the post which inserted the
error, sorry) should I delete this post and write a new one?
| |
|
| On 1 Aug., 11:08, di98mase <di98m...@hotmail.com> wrote:
> On 1 Aug, 10:47, Janis <janis_papanag...@hotmail.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> You are absolutely right, I am sorry for my unclear post. Let me try
> to clarify what I mean.
>
> What I want to do is to remove the line above each line begining with
> the pattern "0000020000",
Given what you've written above (about deleting lines 7 *and* 8)
I suppose you want to remove both lines, not just the one above 8?
> I dont know how the line preceding might
> look, thus I can't search for anything in line 8. Correct about the
> pattern unmatch in my script the pattern is ltu in both the pattern
> and the file. (I did some editing in the post which inserted the
> error, sorry) should I delete this post and write a new one?
Okay, so in your case the solution would be...
BEGIN{RS=3D"";ORS=3D"\n\n"}!/0000020000/
(That's the whole awk program if you wonder.)
If you want to avoid matches of this pattern in any other but the
second line of such a blank separated data block you need some more
code...
BEGIN{RS=3D"";ORS=3D"\n\n";FS=3D"\n"}$2 !~ /0000020000/
(If, OTOH, your "line 7 and 8" example above was wrong and you just
want to delete the _single_ line above the line with that pattern, a
slight modification will be necessary. You could either delete the
first field '$1=3D""' and explicitly print the new block 'print $0' or
buffer each line and output lines delayed. Come back here to ask if
that is the case.)
Janis
| |
| di98mase 2007-08-01, 7:57 am |
| On 1 Aug, 11:58, Janis <janis_papanag...@hotmail.com> wrote:
> On 1 Aug., 11:08, di98mase <di98m...@hotmail.com> wrote:
>
>
>
>
>
>
>
>
>
ot[color=darkred]
>
>
was[color=darkred]
>
>
>
>
>
>
>
>
>
>
>
>
> Given what you've written above (about deleting lines 7 *and* 8)
> I suppose you want to remove both lines, not just the one above 8?
>
>
> Okay, so in your case the solution would be...
>
> BEGIN{RS=3D"";ORS=3D"\n\n"}!/0000020000/
>
> (That's the whole awk program if you wonder.)
>
> If you want to avoid matches of this pattern in any other but the
> second line of such a blank separated data block you need some more
> code...
>
> BEGIN{RS=3D"";ORS=3D"\n\n";FS=3D"\n"}$2 !~ /0000020000/
>
> (If, OTOH, your "line 7 and 8" example above was wrong and you just
> want to delete the _single_ line above the line with that pattern, a
> slight modification will be necessary. You could either delete the
> first field '$1=3D""' and explicitly print the new block 'print $0' or
> buffer each line and output lines delayed. Come back here to ask if
> that is the case.)
>
> Janis- D=F6lj citerad text -
>
> - Visa citerad text -
Wow,
BEGIN{RS=3D"";ORS=3D"\n\n";FS=3D"\n"}$2 !~ /0000020000/
works...so simple but yet so useful...thanks Janis.
Could you explain how it works for me? I understand that you change
the Field Separator (FS), Record Separator (RS) but why do you change
the ORS?
I almost asked you to explain the command to me but after reading more
in the ref. manual i think I got it. the context of your command is...
"Fields are separated by \n, output FS is \n\n so when you find a line
that contains ANYTHING at all BUT /0000020000/ print it."
I just learned a lot...
Thanks again Janis, by the way, what is OTOH?
| |
|
| On 1 Aug., 12:58, di98mase <di98m...@hotmail.com> wrote:
> On 1 Aug, 11:58, Janis <janis_papanag...@hotmail.com> wrote:
>
>
>
>
>
>
>
>
not[color=darkred]
>
>
d was[color=darkred]
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Wow,
>
> BEGIN{RS=3D"";ORS=3D"\n\n";FS=3D"\n"}$2 !~ /0000020000/
>
> works...so simple but yet so useful...thanks Janis.
>
> Could you explain how it works for me? I understand that you change
> the Field Separator (FS), Record Separator (RS) but why do you change
> the ORS?
The "magic" part is the RS=3D"" which defines multi-line blocks as
records instead of the regular single line records.
If you omit the ORS definition you can observe that the newlines
between the blocks are not preserved in the multi-line block mode;
you need another newline to re-create the empty line.
> I almost asked you to explain the command to me but after reading more
> in the ref. manual i think I got it. the context of your command is...
> "Fields are separated by \n, output FS is \n\n so when you find a line
> that contains ANYTHING at all BUT /0000020000/ print it."
>
> I just learned a lot...
>
> Thanks again Janis, by the way, what is OTOH?
A common usenet acronym; On The Other Hand.
Janis
| |
| Thomas Weidenfeller 2007-08-01, 6:58 pm |
| di98mase wrote:
> What I want to do is to remove the line above each line begining with
> the pattern "0000020000", I dont know how the line preceding might
> look, thus I can't search for anything in line 8. Correct about the
> pattern unmatch in my script the pattern is ltu in both the pattern
> and the file. (I did some editing in the post which inserted the
> error, sorry) should I delete this post and write a new one?
flag { print last }
{ last = $0; flag = 1 }
/0000020000/ { flag = 0 }
END { if(flag) print last }
/Thomas
| |
| di98mase 2007-08-03, 3:57 am |
| On 1 Aug, 17:56, Thomas Weidenfeller <nob...@ericsson.invalid> wrote:
> di98mase wrote:
>
> flag { print last }
> { last = $0; flag = 1 }
> /0000020000/ { flag = 0 }
> END { if(flag) print last }
>
> /Thomas
Hmmm...a few questions Thomas:
even all vars are 0 as default should it be a good praxis to define
flag in the BEGIN section flag=0?
then the second line ({ last = $0; flag = 1 }) in you script, should
it just be like that on a single row without any pattern??
also, will this script just remove one instance of the pattern /
0000020000/?
| |
| Thomas Weidenfeller 2007-08-03, 3:57 am |
| di98mase wrote:
> Hmmm...a few questions Thomas:
....
> also, will this script just remove one instance of the pattern /
> 0000020000/?
And here you blew it. Up to that point I was willing to explain things.
But here you indicate you couldn't even be bothered to run this simple
four lines of script code to check if it solves your oh so important
problem.
You want to be spoon-feed? Sorry, I have run out of spoons.
/Thomas
| |
|
| On 3 Aug., 08:58, di98mase <di98m...@hotmail.com> wrote:
> On 1 Aug, 17:56, Thomas Weidenfeller <nob...@ericsson.invalid> wrote:
>
>
>
>
> Hmmm...a few questions Thomas:
>
> even all vars are 0 as default should it be a good praxis to define
> flag in the BEGIN section flag=0?
Contrary to some other programming languages you can rely on the
initialization (which is not quite 0 but defaults to a state that
is interpreted as 0 or "" depending on context).
The way awk is typically used makes it not (not necessarily) good
practice to initialize all variables; mostly the program gets less
legible that way without adding any real safety. Though, there are
programming patterns also in awk where initialization is advised.
> then the second line ({ last = $0; flag = 1 }) in you script, should
> it just be like that on a single row without any pattern??
No pattern means to do the action for all lines. That's basic awk
knowledge.
> also, will this script just remove one instance of the pattern /
> 0000020000/?
Thomas implemented a state machine to solve your problem; it shouldn't
be hard to figure out how it works.
Janis
|
|
|
|
|