how to split/sort file with awk.? 
Author Message
 how to split/sort file with awk.?

I've printspool files containing multiple pages. Every page has a fixed
length of 66 lines. The files contain no formfeeds.
On every page there is a PCL code that specifies the printer output bin.
Now the bins are in order
1,2,3,1,2,3,1,2,3... etc
I want it to be
1,1,..,1,12,2,..2,2,3,3,...3,3

I can do this with a shell script by splitting the job in separate pages,
for every page a grep to evaluate the output bin and concatenate them to
tempfiles for every bin and puting these together at the end. This can be
done easier with awk i gues. (No, i'm sure).
Anybody can get me going with this ?



Tue, 28 Jan 2003 03:00:00 GMT  
 how to split/sort file with awk.?

Quote:

> I've printspool files containing multiple pages. Every page has a fixed
> length of 66 lines. The files contain no formfeeds.
> On every page there is a PCL code that specifies the printer output bin.
> Now the bins are in order
> 1,2,3,1,2,3,1,2,3... etc
> I want it to be
> 1,1,..,1,12,2,..2,2,3,3,...3,3

> I can do this with a shell script by splitting the job in separate pages,
> for every page a grep to evaluate the output bin and concatenate them to
> tempfiles for every bin and puting these together at the end. This can be
> done easier with awk i gues. (No, i'm sure).
> Anybody can get me going with this ?

To pipe pages to three different files depending on the code
on every page:

  ...fetch whole page (maybe getline 66 times?)
  ...fetch code, assuming code is 1,2, or 3
  fn="file"code".tmp"
  print ...lines of page > "file"code".tmp'
  ...

If you care to explain the details of the problem we could be able
to explain th details of a possible solution.

--
  All true believers shall break their eggs at the convenient end.



Tue, 28 Jan 2003 03:00:00 GMT  
 how to split/sort file with awk.?
The way you describe the solution is exactly what i want.

Here are the details. In one file there can be up to 1500 (probably never
more) pages of 66 lines without formfeeds.
On every page there is one code like this:
Esc&l4G for output bin 1 on a mailbox or
Esc&l5G for output bin 2
Esc&l6G for output bin 3

This is used for printing invoices. Originals go into bin1, 1st copy into
bin2, 2nd copy into bin3, and sometimes extra copy's for the customer also
to bin1.
Thus bin order is 1,2,3,1,2,3,1(extra copy),1,2,3,1...
I want it to be 1,1,1(extra),1..1,2,2..,2,3,3..,3,3 because i want to print
it to a printer that doesn't have a mailbox. Why? The problem is that i have
to sort the output manually to make 3 piles. 1 pile for the originals and
extra copys for the customer, 1 pile with copys for the administration
department, and 1 for the division.

To be exact the job has on it's first line codes to initialize the printer.
On the last line are codes to reset the printer.
The first line is one of the 66 lines of the first page.
The last one is a extra line.
I want to split up the job in three parts. One for every bin.
The script should do this

save first line in a buffer       / let us call this init
save last line in a buffer       / let us call this reset
replace first line with empty line
delete last one
split up the file in 3 parts for every bin
replace the first line of every part with the saved init buffer
add to every part the saved reset buffer.
replace in every part all the bin codes with ESC&l12G

After this i can queue the files as separate jobs.

If you can help me with the splitting i can manage the rest.

Thanks Frits

Perique des Palottes heeft geschreven in bericht

Quote:

>> I've printspool files containing multiple pages. Every page has a fixed
>> length of 66 lines. The files contain no formfeeds.
>> On every page there is a PCL code that specifies the printer output bin.
>> Now the bins are in order
>> 1,2,3,1,2,3,1,2,3... etc
>> I want it to be
>> 1,1,..,1,12,2,..2,2,3,3,...3,3

>> I can do this with a shell script by splitting the job in separate pages,
>> for every page a grep to evaluate the output bin and concatenate them to
>> tempfiles for every bin and puting these together at the end. This can be
>> done easier with awk i gues. (No, i'm sure).
>> Anybody can get me going with this ?

>To pipe pages to three different files depending on the code
>on every page:

>  ...fetch whole page (maybe getline 66 times?)
>  ...fetch code, assuming code is 1,2, or 3
>  fn="file"code".tmp"
>  print ...lines of page > "file"code".tmp'
>  ...

>If you care to explain the details of the problem we could be able
>to explain th details of a possible solution.

>--
>  All true believers shall break their eggs at the convenient end.




Tue, 28 Jan 2003 03:00:00 GMT  
 how to split/sort file with awk.?

Quote:

> ...
> If you can help me with the splitting i can manage the rest.

Hope this helps...

BEGIN{
  npage=0
  line1st=""
  linelast=""
  wherelast=""
  f1="file1"
  f2="file2"
  f3="file3"
  npage1=0
  npage2=0
  npage3=0

Quote:
}

{ # for each page, read 66 lines
  line[1]=$0
  for(n=2;n<=66;n++){
    getline line[n]
  }
  page++
  if(page == 1){ # only for the 1st page
    line1st=line[1]
    print line1st > f1  # start file1
    print line1st > f2  # start file2
    print line1st > f3  # start file3
  }
  line[1]=""
  ... specification is incomplete as where in the page
      are those codes...
  if(...Esc&l4G) fn=fn1
  if(...Esc&l5G) fn=fn2
  if(...Esc&l6G) fn=fn3
  ... replace codes by ESC&l12G
  for(n=1;n<=66;n++){
    print line[n] > fn
  }
  wherelast=fn
  linelast=line[66]  # save at the end of every page
  delete line

Quote:
}

END{
  if(wherelast != f1) print linelast > f1  # end file1
  if(wherelast != f2) print linelast > f2  # end file2
  if(wherelast != f3) print linelast > f3  # end file3

Quote:
}

--
  All true believers shall break their eggs at the convenient end.



Tue, 28 Jan 2003 03:00:00 GMT  
 how to split/sort file with awk.?

Quote:

> Hope this helps...

> BEGIN{

Notice the typos page/npage and f1/fn1, f2/fn2, f3/fn3 etc
Code's not been tested.

--
  All true believers shall break their eggs at the convenient end.



Tue, 28 Jan 2003 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Binary Sort/Merge Sort in awk

2. awk problem sorting and deduping file

3. : using sort in awk W/O tmp file.

4. How can I sort a fixed width file using awk on a HP unix systems

5. Regex Groups in String#split (Re: Array#sort!

6. string, split, sort, None, huh?

7. awk and split()

8. Awk question re splitting strings

9. AWK vs PERL - splitting fields

10. An AWK script to split a compilation?

11. Sorting a file w/o SORT verb

12. Outputting info from awk and using the info to name the awk output file

 

 
Powered by phpBB® Forum Software