ric wrote:
> On Apr 7, 4:15 pm, Janis Papanagnou <Janis_Papanag...@[EMAIL PROTECTED]
>
> wrote:
>
>>[ Please don't top-post.]
>>
>>
>>
>>
>>
>>ric wrote:
>>
>>>Hi Kenny,
>>
>>>Well, I'm a AWK newbie, this is my actual code(some friend try to help
>>>me):
>>
>>>~ric)cat script2.awk
>>>#!/usr/bin/awk -f
>>
>>>BEGIN { nStackPtr = -1;}
>>
>>>function push(x) { stack[++nStackPtr] = x; }
>>
>>>function pop() {
>>> if (nStackPtr == -1) return nStackPtr;
>>
>>> return stack[nStackPtr--];
>>> }
>>> {
>>
>>> for (nField = 1;nField <= NF;nField++)
>>> if (match($nField, "<")) {
>>> nPrevious = 0;
>>> for (nLoop = nField-0;nLoop > 0 && nPrevious < 3;nLoop--)
>>> if (!match($nLoop, "<")) {
>>> push($nLoop);
>>> nPrevious++;
>>> }
>>> for (nLoop = 0;nLoop < nPrevious;nLoop++)
>>> printf("%s ", pop());
>>> printf("%s", $nField);
>>
>>> nFollowing = 0;
>>> for (nLoop = nField+0;nLoop < NF && nFollowing < 3;nLoop++)
>>> if (!match($nLoop, "<")) {
>>> printf(" %s", $nLoop);
>>> nFollowing++;
>>> }
>>> printf("\n");
>>
>>> }
>>
>>>} # main
>>>~ric)
>>
>>The main difference between your approach and the program I posted
>>elsethread seems to be that you have introduced some unnecessary
>>stack while my solution builds the result by concatenating strings.
>>(There seem to be a couple of bugs in your program as well.)
>>
>>Janis- Hide quoted text -
>>
>>- Show quoted text -
>
>
>
> hi Janis,
> Ops,sorry for top-post :)
>
> Your solution,it's much better than the first one.
>
>
>>>Your data format makes a solution somewhat ugly (see missing space
after the sentence in " works.This ").
>
> I change the data format, have a space:
>
> #cat file
> I went fishing for some sea <bass> .
> The <bass> part of the song is very moving .
> he proposed an elaborate <program> of public works . This information
> was taken from the magazine.
> the <program> required several hundred lines of code .
>
>
> The only problem is: is printing after the "."
>
> "proposed elaborate program public works This"
>
> and should said:
> "proposed elaborate program public works"
Okay, as you like...
{ gsub (/[,;:]/, " ", $0) ; gsub (/[.]/, " . ", $0) }
/<.*>/ { for (i = 1; i <= NF; i++)
if ($i~/<.*>/) { s = substr ($i, 2, length($i)-2)
c = 0
for (j = i-1; j > 0 && c != 3 && $j != "." ; j--)
if (length($j)>2) { s = $j FS s ; c++ }
c = 0
for (j = i+1; j <= NF && c != 3 && $j != "." ; j++)
if (length($j)>2) { s = s FS $j ; c++ }
}
print s
}
Janis
>
>
> Thanks very much,really
> -ric


|