It is currently Fri, 15 Oct 2021 21:29:49 GMT



 
Author Message
 Create Files Based On Input Records
I want to create output file(s) based on information contained on the input
record.

Input records may look like:

A36X1289 3788 ZX8002349046518F32005XCVB8899 0000000 2346789
ZDF8986576 7898      8976877DN48764987544214 ABC09786U12376598727093758367489888
QST087 8 99076877 9879 98 UX X87657DKL77690097987998989078576676985454537688

I want to create an output file based on cols 30-35 and the names of those
files to be based on the contents of cols 30-35. In this case I would have
three files, F32005, N48764 and X87657, each containing 1 record.

Any ideas? grep, sed, awk gurus?

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
LARRY BACA,                                                       marst2!lbaca
DAASO-VWA AIS, DEFENSE AUTOMATIC ADDRESSING OFFICE, WESTERN DIVISION
DDTC TRACY, TRACY CA. 95376-5057  AUTOVON 462-9391  COMERCIAL 832-9391



 Sat, 26 Feb 1994 07:12:44 GMT   
 Create Files Based On Input Records
From the keyboard of fmd3201@marst2 (Larry Baca):
:I want to create output file(s) based on information contained on the input
:record.
:
:Input records may look like:
:
:A36X1289 3788 ZX8002349046518F32005XCVB8899 0000000 2346789
:ZDF8986576 7898      8976877DN48764987544214 ABC09786U12376598727093758367489888
:QST087 8 99076877 9879 98 UX X87657DKL77690097987998989078576676985454537688
:
:I want to create an output file based on cols 30-35 and the names of those
:files to be based on the contents of cols 30-35. In this case I would have
:three files, F32005, N48764 and X87657, each containing 1 record.
:
:Any ideas? grep, sed, awk gurus?

Pretty much straight out of the perl book; uses just one process, no limit
on length or contents of input records, no limit on number of output files
generated, and reasonably fast to boot:

    #!/usr/bin/perl
    require 'cacheout.pl'
    while (<>) {
        $filename = substr($_, 29, 6);
        if ($filename ne $lastfilename) {
            &cacheout($filename);
            $lastfilename = $filename;
        }
        print $filename $_;
    }

The nice thing about the cacheout routine is that you still get correct
behavior if you have 611 different possible files and NOFILE is only 50,
or whatever.  Furthermore, open()s are minimized -- this wouldn't happen
with the standard sh solution, which would probably also cost you at least
an exec or two per input record.  Gag.

--tom



 Sat, 26 Feb 1994 12:00:43 GMT   
 Create Files Based On Input Records
From the keyboard of Tim.Ouelle...@FtCollins.NCR.COM (Tim.Ouellette):
:nawk '{if(length($0) > 36)
:        {pcFileName = substr($0,30,6);print $0 > pcFileName;}}' datafile
:
:Hope this helps.

Not much -- you run out of file descriptors quickly.

--tom



 Sun, 06 Mar 1994 06:44:08 GMT   
 Create Files Based On Input Records

Larry> I want to create output file(s) based on information contained on the input
Larry> record.

Larry> Input records may look like:

Larry> A36X1289 3788 ZX8002349046518F32005XCVB8899 0000000 2346789
Larry> ZDF8986576 7898      8976877DN48764987544214 ABC09786U12376598727093758367489888
Larry> QST087 8 99076877 9879 98 UX X87657DKL77690097987998989078576676985454537688

Larry> I want to create an output file based on cols 30-35 and the names of those
Larry> files to be based on the contents of cols 30-35. In this case I would have
Larry> three files, F32005, N48764 and X87657, each containing 1 record.

Larry> Any ideas? grep, sed, awk gurus?

try:
nawk '{if(length($0) > 36)
        {pcFileName = substr($0,30,6);print $0 > pcFileName;}}' datafile

Hope this helps.

--
Timothy R. Ouellette                   (303)223-5100 x437
NCR Microelectronics                   Tim.Ouelle...@FtCollins.ncr.com
Ft. Collins, CO.                       uunet!ncrlnk!ncr-mpd!bach!timo
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"If all the world is a stage, I want to run the trap door" -- P. Beaty



 Sat, 05 Mar 1994 16:04:39 GMT   
 Create Files Based On Input Records
In article <1991Sep17.224408.26...@convex.com> you write:

Good point -- but trivially fixed.  You might want to use >> rather than >
if more than one input record goes into the same output file.

nawk '{if(length($0) > 36)
        {pcFileName = substr($0,30,6);print $0 > pcFileName
        close(pcFileName)

Regards,
John Levine, jo...@iecc.cambridge.ma.us, {spdcc|ima|world}!iecc!johnl



 Mon, 07 Mar 1994 07:45:46 GMT   
 Create Files Based On Input Records
Tom> Nntp-Posting-Host: pixel.convex.com

Tom> From the keyboard of Tim.Ouelle...@FtCollins.NCR.COM (Tim.Ouellette):
Tom> :nawk '{if(length($0) > 36)
Tom> :        {pcFileName = substr($0,30,6);print $0 > pcFileName;}}' datafile
Tom> :
Tom> :Hope this helps.

Tom> Not much -- you run out of file descriptors quickly.

Tom> --tom

Minor oversight. Simply close the file after your done printing to it.
nawk '{if(length($0) > 36)
        {pcFileName = substr($0,30,6);print $0 > pcFileName;close(pcFileName);}}' datafile

-Tim
--
Timothy R. Ouellette                   (303)223-5100 x437
NCR Microelectronics                   Tim.Ouelle...@FtCollins.ncr.com
Ft. Collins, CO.                       uunet!ncrlnk!ncr-mpd!bach!timo
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"If all the world is a stage, I want to run the trap door" -- P. Beaty



 Mon, 07 Mar 1994 15:13:02 GMT   
 Create Files Based On Input Records
From the keyboard of Tim.Ouelle...@FtCollins.NCR.COM (Tim.Ouellette):
:>>>>> On 17 Sep 91 22:44:08 GMT, tchr...@convex.COM (Tom Christiansen) said:
:Tom> Nntp-Posting-Host: pixel.convex.com
:
:Tom> From the keyboard of Tim.Ouelle...@FtCollins.NCR.COM (Tim.Ouellette):
:Tom> :nawk '{if(length($0) > 36)
:Tom> :        {pcFileName = substr($0,30,6);print $0 > pcFileName;}}' datafile
:Tom> :
:Tom> :Hope this helps.
:
:Tom> Not much -- you run out of file descriptors quickly.
:
:Tom> --tom
:
:Minor oversight. Simply close the file after your done printing to it.
:nawk '{if(length($0) > 36)
:        {pcFileName = substr($0,30,6);print $0 > pcFileName;close(pcFileName);}}' datafile

Oh?  How are you going to guarantee that

    1) The file can contain multiple records?
    2) The file contains no old data?

If you use >, you can only have one record.
If you use >>, you will get stuff from the last run.

Sometimes, simple solutions aren't.  

And I didn't even mention that this is about as efficient as
doing an "echo >> file" each time, that is, not at all.

--tom



 Mon, 07 Mar 1994 23:56:45 GMT   
 Create Files Based On Input Records
In <9109181945.AA02...@iecc.cambridge.ma.us> jo...@iecc.cambridge.ma.us (John R. Levine) writes:

How about sorting the inputfile before passing it to awk. E.g.: (not tested)

sort -t"\t" +0.30 -0.35 datafile| # Works only if the inputfile contains no tabs
nawk '{if(length($0) > 36)
        {pcFileName = substr($0,30,6);print $0 >> pcFileName
        if (pcFileName != Lastfile)
                close(pcFileName)
        Lastfile = pcFileName
END { close(pcFileName) }'

The first outputfile of the sorted inputfile is more often closed as needed,
but this is minimal overhead.

--
Peter Cleve, Hildesheimer Str. 316, 3014 Laatzen 3, Tel. 05102/1854 (Voice)
  Energiesparen ist das moralische Aequivalent zum Krieg. (Jimmy Carter)



 Wed, 09 Mar 1994 02:17:58 GMT   
 Create Files Based On Input Records

OK, so do it the hard way:

#! /bin/sh
cat datafile | while read i
do
    echo $i | nawk '{if(length($0) > 36)
        {pcFileName = substr($0,30,6);print $0 > pcFileName;}}'
done

Not very elegant, but it works.
--
                    If you're not winning - change the rules
       Dan Flak - McCaw Cellular Communications Inc., 5400 Carillon Point
   Kirkland, Wa 98033-7397, 206-828-8006, (usenet: flak%mc...@nwnexus.wa.com)



 Tue, 08 Mar 1994 03:12:32 GMT   
 Create Files Based On Input Records

John> In article <1991Sep17.224408.26...@convex.com> you write:

John> Good point -- but trivially fixed.  You might want to use >> rather than >
John> if more than one input record goes into the same output file.

John> nawk '{if(length($0) > 36)
John>         {pcFileName = substr($0,30,6);print $0 > pcFileName
John>        close(pcFileName)
John> }}' datafile

John> Regards,
John> John Levine, jo...@iecc.cambridge.ma.us, {spdcc|ima|world}!iecc!johnl

John
   Thanks for correcting my oversight.  

-Tim

Timothy R. Ouellette                   (303)223-5100 x437
NCR Microelectronics                   Tim.Ouelle...@FtCollins.ncr.com
Ft. Collins, CO.                       uunet!ncrlnk!ncr-mpd!bach!timo
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"If all the world is a stage, I want to run the trap door" -- P. Beaty



 Mon, 07 Mar 1994 21:33:31 GMT   
 Create Files Based On Input Records
From the keyboard of f...@mcgp1.UUCP (Dan Flak):
:
:OK, so do it the hard way:
:
:#! /bin/sh
:cat datafile | while read i
:do
:    echo $i | nawk '{if(length($0) > 36)
:        {pcFileName = substr($0,30,6);print $0 > pcFileName;}}'
:done
:
:Not very elegant, but it works.

It does?  Looks like you don't get more than one line per output file.

--tom



 Thu, 10 Mar 1994 13:36:01 GMT   
 
   [ 11 post ] 

Similar Threads

1. Split input file based on size of file

2. Reading records from file and inputting in a template

3. splitting a very large file based on characters in a record (performance)

4. script for feeding a record each time from a data base file

5. How to create a single patch input file?

6. Creating a base SysV init file system

7. retrieve record from record file(binary) to output to disply, need help

8. standard input instead of input file


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software