[ 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


|