Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2007
    Posts
    21

    Thumbs up Unanswered: Shell Script for the following problem

    Guru's,
    I am having a problem to write a shell script for the following problem,I will be writing the shell scripts for the first time so having some problem initially.I will appericiate your help in guiding me to solve this.

    Ticket1--
    02 00 00 34 41 00 00 70 81
    00 14 20 37 00 53 63 00 00
    09 18 23 00 45 00 00 79 00

    Ticket2---

    03 11 00 00 00 52 67 00 86
    00 00 21 00 42 00 69 75 89
    06 00 28 39 47 55 00 00 00

    Ticket3---

    00 15 00 00 00 51 64 71 85
    04 17 00 31 46 00 68 00 00
    00 00 29 38 00 57 00 76 90


    Ticket4----

    05 00 26 00 40 00 61 77 00
    00 12 27 00 00 58 65 00 83
    07 00 00 36 48 59 00 00 84

    Ticket5---

    00 10 24 32 00 00 60 72 00
    01 16 25 00 00 00 66 00 80
    00 00 00 35 44 54 00 78 82

    The shell script to write is this------

    we have some 200thousand values like this.
    These 6 tickets create a strip.I have to write a shell script like this

    1.. There should be 15 entries in a ticket.Even though there are 27 entries but 00 we need to neglect they are just to fill the gap.
    2.. No entry should get repeated in a ticket.
    3.. In one row we should have only 5 entries.Even though there are 9 entries we should neglect 00,they are just to fill.
    4.. Following the ticket pattern pasted above In a row or column if there are two values then they should be in ascending order.

    If you have any more doubts please let me know.

    This seems to me a bit tricky so i need the help of the experts of this field.
    I appericiate your help in this regard.

    So many thanks in advance.
    Last edited by namishtiwari; 08-11-07 at 05:58.

  2. #2
    Join Date
    Aug 2007
    Posts
    21

    Thumbs up

    The format of the data will be like this--

    00,11,21,00,00,55,00,73,83,07,00,22,39,43,00,66,00 ,00,00,15,00,00,44,58,67,00,87
    02,14,24,30,00,00,00,70,00,00,00,00,32,47,50,60,74 ,00,04,16,27,38,00,00,00,00,89
    00,00,00,00,42,52,61,71,86,01,00,29,33,00,59,00,75 ,00,03,19,00,00,45,00,68,00,88
    00,13,20,31,00,00,64,76,00,05,00,00,35,41,56,00,00 ,81,00,00,28,00,00,57,65,79,90
    06,10,00,36,40,00,00,00,80,00,12,25,00,00,51,62,72 ,00,09,00,00,37,49,00,69,00,84
    00,17,23,00,46,53,00,77,00,08,00,00,34,00,54,00,78 ,82,00,18,26,00,48,00,63,00,85
    00,14,23,39,00,00,60,00,87,02,00,28,00,41,50,00,75 ,00,03,15,00,00,44,00,68,00,90
    00,16,25,35,00,53,00,71,00,08,00,00,36,40,00,62,00 ,88,00,00,27,37,00,58,64,78,00
    05,10,00,00,48,52,00,73,00,00,00,24,31,00,57,66,00 ,81,07,17,00,00,49,00,00,79,85
    00,00,20,33,00,00,63,70,80,06,12,00,34,42,56,00,00 ,00,00,00,29,00,00,59,69,74,86
    04,13,00,32,43,00,65,00,00,00,18,22,00,00,55,00,72 ,84,09,00,00,38,47,00,67,00,89
    01,11,21,00,00,51,00,76,00,00,00,26,30,45,54,00,00 ,82,00,19,00,00,46,00,61,77,83
    06,11,22,00,00,00,64,00,81,00,00,00,30,40,56,00,79 ,85,08,15,27,35,00,59,00,00,00

  3. #3
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    1.. There should be 15 entries in a ticket.
    I count 27 entries on every line.

    2.. No entry should get repeated.
    00 appears ten times on the first line.

    3.. In one row we should have only 5 entries.
    I count 27 entries on every line.
    If bunches of these group up into a single entry then how does 27 split into 5 entries?

    4.. In a row or column if there are two values then they should be in ascending order.
    The last column of figures doesn't do this.

    I will be writing the shell scripts for the first time so having some problem initially
    So what have you done so far - perhaps we can work it out from that

    Mike

  4. #4
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    248
    What exactly is your problem? From your previous thread I understand that the records are to be stored in a SQL database. Do you have to check for uniqueness before the data is inserted? And what should happen if they're not? Or do jou have to generate unique numbers randomly with a script or a program and store those in a database table?

    Hans

  5. #5
    Join Date
    Aug 2007
    Posts
    21

    Red face

    [QUOTE=mike_bike_kite]I count 27 entries on every line.
    Right,but the ticket format which i pasted earlier we need to follow that,
    yes there are 27entries but ,in a row there are 9 columns and exactly 5 entries should be there not more than that.
    In a column there should be min 1 entry and that only should be in ascending order.


    00 appears ten times on the first line.
    We need not to consider 00 for this problem they are just to fill fields,ignore that.

    I count 27 entries on every line.
    If bunches of these group up into a single entry then how does 27 split into 5 entries?
    27 split into 15entries rest are put as zero but neglect that entries.

    The last column of figures doesn't do this.
    That is what we need to check.

    So what have you done so far - perhaps we can work it out from that
    Now you are clear of the problem or need some more explanation.

    Thanks for the comments.

  6. #6
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    You should probably explain a bit more what you want to do. As Tyveleyn said, it is unclear what exactly you should do. We don't know the exact semantics of the following terms and how the data for those is represented:
    • ticket - what comprises a ticket?
    • strip - what is a strip?
    • entry - what is an entry in the list and how does it relate to strip and ticket?


    p.s: Is this by any chance an exercise from school? It looks like one...
    Knut Stolze
    IBM DB2 Analytics Accelerator
    IBM Germany Research & Development

  7. #7
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    namishtiwari,
    Pls refrain from posting to multiple forums leaching for solutions without clear indication of your own attempt at solving the problem!
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  8. #8
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    The following awk program read the tickets_file and produces two files :
    tickets_file.ok Valid tickets
    tickets_file.err Invalid tickets

    Awk program : check_tickets.awk
    Code:
    #!/usr/bin/awk -f
    
    #==========================================================================
    # Functions
    #==========================================================================
    
    #-------------------------------------
    # Print Ticket end errors
    #-------------------------------------
    
    function print_last_ticket(    file, i) {
    
       if (Ticket == "") return;
    
       # Set output file depending on error detection
    
       file = (Ticket_errors ? Ticket_file_err : Ticket_file_ok);
    
       # Print ticket lines and errors
    
       for (i=1; i<=Ticket_lines_count; i++) {
          print Ticket_lines[i] > file;
          if (Ticket_lines_err[i]) print Ticket_lines_err[i] > file;
          delete Ticket_lines[i];
          delete Ticket_lines_err[i];
       }
       return;
    }
    
    #-------------------------------------
    # Init infos for a new ticket
    #-------------------------------------
    
    function init_new_ticket(id    ,i) {
       Ticket_errors      = 0;
       Ticket_rows        = 0;
       Ticket_lines_count = 0;
       Ticket             = id;
       for (i=1; i<=99; i++)
          Ticket_entries[i] = 0;
       return;
    }
    
    #-------------------------------------
    # Add and error for the ticket line
    #-------------------------------------
    
    function add_error(text    ,err) {
       err = Ticket_lines_err[Ticket_lines_count];
       Ticket_lines_err[Ticket_lines_count] = err (err ? "\n" : "") "^- " text;
       ++Ticket_errors;
    }
    
    #==========================================================================
    # Proceed Input Records
    #==========================================================================
    
    #-------------------------------------
    # New ticket
    #-------------------------------------
    
    /^Ticket/ {
       print_last_ticket();
       init_new_ticket($1);
    }
    
    #-------------------------------------
    # First record of file
    #-------------------------------------
    
    FNR==1 {
       Ticket_file_ok  = FILENAME ".ok"   # Valid tickets file
       Ticket_file_err = FILENAME ".err"  # Invalid tickets file
    
    }
    
    #-------------------------------------
    # Ignore records before first ticket
    #-------------------------------------
    
    ! Ticket {
       next;
    }
    
    #-------------------------------------
    # Memorize ticket line
    #-------------------------------------
    
    {
       Ticket_lines[++Ticket_lines_count] = $0;
    }
    
    #-------------------------------------
    # Entries row
    #-------------------------------------
    
    NF>1 {
    
       # Error If entries row count > 3
    
       if (++Ticket_rows > 3) {
          add_error( "Too many entries rows: " Ticket_rows);
       }
    
       # Error if entries count not equal to 9
    
       if (NF != 9) {
          add_error( "Invalid entries count: " NF);
       }
    
       # Check every entry
    
       entries_count = 0;
       last_entry   = 0;
       for (i=1; i<=NF; i++) {
    
          # Error if not numeric value, proceed next entry
    
          if ($i !~ /^[0-9][0-9]$/) {
             add_error( "Entry #" i " not numeric: " $i);
             continue;
          }
    
          # Ignore 00 entry
    
          if ($i == "00") continue;
    
          entries_count++;
    
          # Error if not ascending order
    
          if ($i <= last_entry) {
             add_error( "Entry #" i " out sequence: " $i "<=" last_entry );
          }
          last_entry = $i;
    
          # Error if duplicated entry
    
          if (++Ticket_entries[$i] > 1) {
             add_error("Entry #" i " duplicated: " $i);
          }
    
       }
    
       # Error if entries count in row is not equal to 5
    
       if (entries_count != 5) {
          add_error( "Invalid entries count in row: " entries_count);
       }
    
    }
    
    #-------------------------------------
    # End of files
    #-------------------------------------
    
    END {
       print_last_ticket();
    }
    Example
    Code:
    $ cat tickets.txt
    Ticket1--
    02 00 00 34 41 00 00 70 81
    00 14 20 37 00 53 63 00 00
    09 18 23 00 45 00 00 79 00
    
    TicketErr2---
    
    03 11 00 00 00 52 67 00 86
    00 00 21 00 42 00 69 75 89
    06 00 28 39 47 55 00 00 00
    12 00 13 38 00 66 00 00 00
    
    TicketErr3---
    
    00 15 00 00 00 51 64
    04 17 00 31 46 00 68 00 00 0
    XX 00 29 38 00 57 00 76 90
    
    
    TicketErr4----
    
    05 00 26 00 25 00 61 77 00
    00 12 27 00 29 58 65 00 83
    07 00 00 36 48 59 00 00 00
    
    TicketErr5---
    
    00 10 24 32 00 00 60 72 00
    01 10 25 00 00 00 66 00 80
    00 00 00 32 44 54 00 78 82
    
    $ awk -f check_tickets.awk tickets.txt
    $ cat tickets.txt.ok
    Ticket1--
    02 00 00 34 41 00 00 70 81
    00 14 20 37 00 53 63 00 00
    09 18 23 00 45 00 00 79 00
    
    $ cat tickets.txt.err
    TicketErr2---
    
    03 11 00 00 00 52 67 00 86
    00 00 21 00 42 00 69 75 89
    06 00 28 39 47 55 00 00 00
    12 00 13 38 00 66 00 00 00
    ^- Too many entries rows: 4
    ^- Invalid entries count in row: 4
    
    TicketErr3---
    
    00 15 00 00 00 51 64
    ^- Invalid entries count: 7
    ^- Invalid entries count in row: 3
    04 17 00 31 46 00 68 00 00 00
    ^- Invalid entries count: 10
    XX 00 29 38 00 57 00 76 90
    ^- Entry #1 not numeric: XX
    
    
    TicketErr4----
    
    05 00 26 00 25 00 61 77 00
    ^- Entry #5 out sequence: 25<=26
    00 12 27 00 29 58 65 00 83
    ^- Invalid entries count in row: 6
    07 00 00 36 48 59 00 00 00
    ^- Invalid entries count in row: 4
    
    TicketErr5---
    
    00 10 24 32 00 00 60 72 00
    01 10 25 00 00 00 66 00 80
    ^- Entry #2 duplicated: 10
    00 00 00 32 44 54 00 78 82
    ^- Entry #4 duplicated: 32
    $
    Jean-Pierre.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •