0

Currently, I'm using a process to create a very specific set of four files. The Process I'm using is shown in the attached Process Image, below:

Process Image

The first line is part of a solution, given to a previous question about Randomized Serialized Numbers.

perl -e 'for(RANGESTART..RANGESTOP){$x = $_ + 0; print "$x" . "','\n";}' > filename.csv

As you can see, RANGESTART is generally something like 30000000 RANGESTOP would generally be something like 30010000, giving me 10,000 numbers, counting up from 30,000,000.

For the purpose of validation (Regulator Body Requirement), I have to perform a check, to make sure the numbers generated are unique and never duplicated. This validation must create a secondary file (filename-sort.csv). To accomplish this, I'm using:

sort -u -o filename1.csv filename2.csv

At this point I have two files.

I have to use a 3rd Party Application (Seagull Scientific's Bartender), for the purpose of printing labels, on a production line. One side of the line needs a file with only even numbers, the other side needs a file with only odd numbers. To accomplish this, I'm using:

awk '{print>sprintf("%sfile.csv",NR%2?"even":"odd",PROCINFO["pid"])}' filename.csv

Now I have my four files.

These four files are a requirement, to appease a potential audit, by a Regulatory Body.

This process works fine, but requires my direct involvement, every time a new number set is needed.

My question has two parts:

Part A: I'd like to do this all with three individual perl commands, much the same way I'm already doing this with perl, sort and awk.

Part B: I'd like to be able to combine those three perl commands, into a single perl script, leaving the three perl commands from Part A, in-tact in their original form.

I imagine this perl script would ask a user for a starting number, an ending number and 8-digit date (YYYYMMDD). As the script completes, it should ask for the user's input to confirm the creation of the first file (YYYYMMDD.csv), confirm creation of the sorted file (YYYYMMDD-sort.csv) and confirm creation of even/odd files (evenfile.csv & oddfile.csv).

I imagine the file creation prompts working something like this:

  1. Enter "Y" or "YES" to create YYYYMMDD.csv (Any other answer should exit the script).
  2. Enter "Y" or "YES" to create YYYYMMDD-sort.csv (Any other answer should exit the script).
  3. Enter "Y" or "YES" to create evenfile.csv and oddfile.csv (Any other answer should exit the script).

Finally, I'd like this perl script to be able to run from Apple's Terminal, Linux Terminal and Windows 7 Pro CMD Line.

Am I asking too much? If so why? If not, how?

  • 1
    The immediate and obnoxious question is "what have you tried?" You are asking too much if you expect us to do your work for you for free. – glenn jackman Apr 22 '16 at 0:58
  • @glennjackman I don't feel your question was obnoxious. I use Linux daily and have some familiarity with awk/sort. However, I haven't used perl to accomplish anything, in the past, unless it was part of a package installation script... until now. I've been reading about perl's ability to work across computing platforms (which is the only reason I've asked about using perl). I pretty much expected your answer, due to how in-depth my question was, just didn't know who it would come from. Maybe someone could just help me address Part A, how to replace my sort and awk commands, with perl. – bamajr Apr 23 '16 at 12:23
  • I don't understand the sorting requirement. The first perl program outputs the numbers in numerical order. What about that requires sorting? – glenn jackman Apr 23 '16 at 17:56
  • @glennjackman I have a requirement that states "generated numbers (number list) must be checked for duplicate numbers, by a secondary process, and create a second file for verification purposes." The "sort" command I'm currently using is doing just that - sorting for unique entries and outputting a second file. – bamajr Apr 23 '16 at 18:36
0

Looks like your work can be reduced to:

perl -e '
    use POSIX qw(strftime);

    my ($start, $num) = (30_000_000, 10_000);
    my @all   = ( $start .. $start + $num -1 );  # 10,000 numbers
    my @odds  = grep { $_ % 2 == 1 } @all;
    my @evens = grep { $_ % 2 == 0 } @all;

    my $filename = strftime "%Y%m%d", localtime;
    my $fh;
    open $fh, ">", "$filename.csv";       print $fh join("\n", @all), "\n";   close $fh;
    open $fh, ">", "$filename-sort.csv";  print $fh join("\n", @all), "\n";   close $fh;
    open $fh, ">", "$filename-odds.csv";  print $fh join("\n", @odds), "\n";  close $fh;
    open $fh, ">", "$filename-evens.csv"; print $fh join("\n", @evens), "\n"; close $fh;
'

As I said, I don't understand the need to have a sorted file, since the file is already sorted.

  • Before I mark this answered, I have a couple questions... Am I correct in assuming I'd need to save this as a .pl file and add the hash-bang '#!/usr/bin/perl' line? Would I also need the 'use strict; use warnings;' lines? Finally, the point of filename-sort.csv is for their to be a second file, created by sorting the first file, to confirm the uniqueness of the numbers in the list. In an audit situation the two separate processes and their files can be provided, to show what was done to create and verify unique numbers. Is this happening in your solution? – bamajr Apr 25 '16 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.