How do I get data printed in the order I want? 
Author Message
 How do I get data printed in the order I want?

I print some things in the BEGIN section. Then, while looping over a file, I
print items and sort them. Then, in the END, I print some more. But, the
problem is getting these sections in the right order. Pseudocode and results
below. Any thoughts on how to force the prints to come out in the right
order (marker1, marker2, sorted lines, marker3)? Thanks.

BEGIN {
  print "marker1"
  print "marker2"

Quote:
}

 { print $0 | "sort -nr +3" }

END {
  close("sort -nr +3")
  print "marker3"

Quote:
}

Output :

< sorted $0 lines >
marker1
marker2
marker3



Sat, 10 Aug 2002 03:00:00 GMT  
 How do I get data printed in the order I want?


Quote:
>I print some things in the BEGIN section. Then, while looping over a file, I
>print items and sort them. Then, in the END, I print some more. But, the
>problem is getting these sections in the right order. Pseudocode and results
>below. Any thoughts on how to force the prints to come out in the right
>order (marker1, marker2, sorted lines, marker3)? Thanks.

>BEGIN {
>  print "marker1"
>  print "marker2"
>}

> { print $0 | "sort -nr +3" }

>END {
>  close("sort -nr +3")
>  print "marker3"
>}

>Output :

>< sorted $0 lines >
>marker1
>marker2
>marker3

sort -nr +3 infile | awk 'BEGIN {print "marker1"; print "marker2"}
                          {print} END {print "marker3"}'

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sat, 10 Aug 2002 03:00:00 GMT  
 How do I get data printed in the order I want?
Thanks, but it's a little more complicated than the example. The print that
is piped to the quoted sort really has some more processing going on, so I
need awk code to decide whether to print this or not. I don't want to just
sort an entire file. The close in the END section effectively separates the
sorted lines from marker3, but I need a way to separate marker1,2 from the
sorted lines. In The AWK Programming Language, p. 59, close is presented as
a way to close files or pipes. In the BEGIN section, what is the file or
pipe name I can use to flush these prints? Is this a UNIX issue?

Quote:



>>I print some things in the BEGIN section. Then, while looping over a file,

I
<deleted>
Quote:
>sort -nr +3 infile | awk 'BEGIN {print "marker1"; print "marker2"}
>   {print} END {print "marker3"}'



Sat, 10 Aug 2002 03:00:00 GMT  
 How do I get data printed in the order I want?


Quote:
>Thanks, but it's a little more complicated than the example. The print that
>is piped to the quoted sort really has some more processing going on, so I
>need awk code to decide whether to print this or not. I don't want to just
>sort an entire file. The close in the END section effectively separates the
>sorted lines from marker3, but I need a way to separate marker1,2 from the
>sorted lines. In The AWK Programming Language, p. 59, close is presented as
>a way to close files or pipes. In the BEGIN section, what is the file or
>pipe name I can use to flush these prints? Is this a UNIX issue?




>>>I print some things in the BEGIN section. Then, while looping over a file,
>I
><deleted>
>>sort -nr +3 infile | awk 'BEGIN {print "marker1"; print "marker2"}
>>   {print} END {print "marker3"}'

I think you want to look at the system() function.

I'd write the stuff you want to sort to a separate file, sort it,
and then wrap it with the markers.

awk 'BEGIN(print "marker1" > "file1";print "marker2" > "file2";
           close("file1")}
     {print > "file2"}
     END{ close("file2");cmd="sort file2 >> file1";system(cmd);
          close(cmd);print "marker3" >> "file1"; close("file1")"' infile

The above is untested.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sat, 10 Aug 2002 03:00:00 GMT  
 How do I get data printed in the order I want?


Quote:
>Thanks, but it's a little more complicated than the example. The print that
>is piped to the quoted sort really has some more processing going on, so I
>need awk code to decide whether to print this or not. I don't want to just
>sort an entire file. The close in the END section effectively separates the
>sorted lines from marker3, but I need a way to separate marker1,2 from the
>sorted lines. In The AWK Programming Language, p. 59, close is presented as
>a way to close files or pipes. In the BEGIN section, what is the file or
>pipe name I can use to flush these prints? Is this a UNIX issue?




>>>I print some things in the BEGIN section. Then, while looping over a file,
>I
><deleted>
>>sort -nr +3 infile | awk 'BEGIN {print "marker1"; print "marker2"}
>>   {print} END {print "marker3"}'

Or you could do this:

awk '{...whatever wou wanted to do...}' infile | sort |
awk 'BEGIN{print "marker1";print "marker2"}
     {print}
     END{print "marker3"}' > outfile

The first awk selecting what to print before sorting, the second one
adding the markers, before and after it.

You could do the second awk stuff with shell stuff instead.

I showed a way to do this all in one awk in another post.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sat, 10 Aug 2002 03:00:00 GMT  
 How do I get data printed in the order I want?
Quote:

> I print some things in the BEGIN section. Then, while looping over a file, I
> print items and sort them. Then, in the END, I print some more. But, the
> problem is getting these sections in the right order.

> BEGIN {
>   print "marker1"
>   print "marker2"
> }

>  { print $0 | "sort -nr +3" }

> END {
>   close("sort -nr +3")
>   print "marker3"
> }

> Output :

> < sorted $0 lines >
> marker1
> marker2
> marker3

A techinque I learned long ago (for printing multiple reports in one
pass) would be applied like this -- first modify the awk code as
indicated;
 BEGIN {
   print "1 " "marker1"
 #       ^^^^
   print "1 " "marker2"
 #       ^^^^
 }

  { print "2 " $0 }| "sort -nr +3" }
 #        ^^^^
 END { print "3 " "marker3" }
 #           ^^^^

Then run in as follows
 awk -f code infile | sort +1 -1 -nr +4 | awk "{print substr($0,3)}"

By adding then deleting an extra key, in this case the section number,
we cal let the system sort order the lines the way we want. BTW, a
stable sort is assumed; if not then the keys for the first and last
sections are more complex.

--
Bob Stearns
University of Georgia

(706)542-5110



Sun, 11 Aug 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. compiles done in wrong order

2. Print Labels OK in Preview, Missing Data When Printed

3. Getting a program to inform you when it's done

4. Doing emacs bindings or getting modifiers

5. HttpDist - getting errors, What I'm doing wrong

6. Doing emacs bindings or getting modifiers

7. Plot/print data in a VI while this data is updated in a CIN

8. Getting applications in order…

9. Evaluation order of multiple Gets

10. DBT 's bigger than 2 Mb getting corrupted and out of order

11. getting the next widget from the tabbing order

12. wanted: lisp program doing syntactic analysis of a frame langage

 

 
Powered by phpBB® Forum Software