Tricky Shell script

The Problem is mentioned below

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

[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 :S
Now you are clear of the problem or need some more explanation.

Thanks for the comments.

I have to write a shell script for the following problem.I am a novice in this area.Please help me out all Guru's.

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

I have to write a shell script like this...

  1. In a row there are 9 entries but there should be exactly 5 entries in a row.
    We need to neglect 00 they are just to fill the positions but of no use so ignore them.,

  2. In a column there should be minimum 1 entry and if more than that then in ascending order only.

  3. There should be total 15 entries in all even though thare are 27 but 00 a need not be considered they are just to fill the positions.

If you have any doubt please let me know.
I will appericiate your help in this regard.

Thanks in Advance
Namish

is it home work

I think you have opened mutiple threads for this which is against the forum rules.

I have the following script written for this,as i told i am novice in this shell scripting field so i need some guidance,There are erros when i ran the script but its hard for me to track them.

       echo "enter the file name"
       read file
       k=1
       n=1,o=10,p=19
       for firstline in $file
       do
       firstline = `sed -n $k p $file`
       num1 = `cut -f$n -d "," -s $firstline` 
       num2 = `cut -f$o -d "," -s $firstline`
       num3 = `cut -f$p -d "," -s $firstline`

        
      if [ $num1 -lt $num2 && $num1 -lt $num3 && $num2 -lt $num3 ]
       then
       echo " Numbers in column are in ascending order"
        else
          echo " Numbers not in ascending order in columns"
          n = `expr $n + 1`
          o = `expr $o + 1`
          p = `expr $p + 1`
      fi

        echo " To check the total numbers in a row "
        for a in firstline
         do
          a = `cut -f1-9 -d  "," -s $firstline`
          count=0
          digits = `grep [0-9][1-9] a`
           count = `expr $count + 1`
           echo " The total number of digits in a row is $count "
           done
     k = `expr $k + 1`
 
     echo "To check total numbers in an entry"

       for tdigits in firstlineline
        do
         count=0
         tdigits = `grep [0-9][1-9] firstline`
         echo "The total Numbers of digits are $count"
         count = `expr $count + 1`
         done
        echo " We have completed the task,at the end of program"
 done

[/quote]

please use the provided code tags for code.

i could get only the requirement 3

This code prints the lines if there are 15 fields ignoring the fillers "00"

#!/bin/ksh

inpFile=$1

for line in `cat $inpFile`
do
        count_fields=$(echo $line | sed "s/00,*//g;s/,$//g" | wc -c)
        if [[ $count_fields -eq 45 ]]
        then
                print $line
        fi
done

lorcan,
thanks for your input.

The rest two requiremnts are ---

  1. In a row there are 9 entries but there should be exactly 5 entries in a row.
    We need to neglect 00 they are just to fill the positions but of no use so ignore them.,

  2. In a column there should be minimum 1 entry and if more than that then in ascending order only.

Can you suggest what u did not get in this.
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

The ticket numbers shown above have 9 columns and 3 rows.
The 9 rows must have exactly 5 numbers(non 0) rest are filled with 00.we need to check that.

In a column there should be minimum 1 entry and if more than one entry is there then they should be in ascending order.In this ticket for reference you can easily see that in a column if numbers are more than two in a column then they are in ascending order,i need to make a check for that also.

I would like your suggestion also for my script.I am using Solaris system.

Havent tested it, You can give a try

#!/bin/ksh

inpFile=$1
outFile="$HOME/Output.txt"

modFile="/tmp/mod_inputfile.txt"
tmpFile1="/tmp/sample1.txt"
tmpFile2="/tmp/sample2.txt"

TOTAL_FILEDS=9
COUNT=1
rm -f $outFile
sed 's/ /,/g' $inpFile > $modFile
for line in `cat $modFile`
do
        count_fields=$(echo $line | sed "s/00,*//g;s/,$//g" | wc -c)
        if [[ $count_fields -eq 15 ]]
        then
                echo $line >> $outFile
        fi
done
rm -f $modFile
while [[ $COUNT -le $TOTAL_FILEDS ]]
do
        cut -d',' -f$COUNT $outFile | grep -v 00 > $tmpFile1
        cut -d',' -f$COUNT $outFile | grep -v 00 | sort > $tmpFile2
        diff $tmpFile1 $tmpFile2 > /dev/null
        if [[ $? -eq 1 || ! -s $tmpFile1 ]]
        then
                echo "Check the $COUNT field of the INPUT FILE"
                exit 126
        fi
        COUNT=$((COUNT+1))
done
echo "Success.... Check the output in $outFile"
rm -f $tmpFile1 $tmpFile2

Hi lorcan,
First thanks for the input you provided,when i ran that script it telling the following error--

cut: cannot open /home/NamishT/Output.txt
cut: cannot open /home/NamishT/Output.txt
Check the 1 field of the INPUT FILE

I just commented the rm -f outFile instead its telling the same problem,what can be the problem here.

Thanks in advance

Might be the output file is not created based on the condition in the if loop.
Can you run the script in debug mode

lorcan,
i put the debug statements in the if condition,its not going there.i guess the problem is in counting the fields.that statement itself not giving the desired output.
Thanks

When i checked the count of fields with your input it gives 15.
Check below

echo "02 00 00 34 41 00 00 70 81" | sed "s/ /,/g;s/00,*//g;s/,$//g" | wc -c

lorcan,
When i ran the script it tells

./testingscript testingdata
cat: cannot open /export/home/niit-dev/Output.txt
The contents of the outFile are
The count_fields is 45
The count_fields is 45
The count_fields is 45
The count_fields is 45
The count_fields is 45
The count_fields is 45
The count_fields is 45
The count_fields is 45
cut: cannot open /export/home/niit-dev/Output.txt
cut: cannot open /export/home/niit-dev/Output.txt
Check the 1 field of the INPUT FILE

My input file is ---
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

In this the total number of count should be 15,
when i ran with my input file it shows 45 numbers whether it should be 15 because only 15 digits are there leaving all 00.

while what u suggested that only have 5 digits leaving all 00,means that should show 5 numbers only but the input file which u r follwing in that 3 rows make a line for testing purpose so that whole line should have total 15 numbers...

still it shows the same error outFile cannot be opened.
I am also trying to debug but being a novice i am not finding it.
can you try that with the inputfile which i provided.

For this req i had written the script. For other it was already complete right. I hope you use two input files one with 9 entries and other as mentioned above

lorcan,
Thanks for the inputs first.
i guess you did not get the requirement fully.

i mentioned clearly that in a row there should be exactly 5 entries and total for ticket it shoud be 15.E.g.

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

This is a ticket and in a row there should be only 5 entries we should ignore the 00,and totally it should be 15 for a ticket ignoring all 00.Like this(Ticket) there are 200thousand values.

Did you get it?..

This script checks for the 15 tickets in a row and also checks if there are 5 tickets within every 9 columns.

And as a whole i am checking whether there are atleast one ticket in a column and arranged in ascending order.

#!/bin/ksh

inpFile=$1
outFile="$HOME/Output.txt"

tmpFile1="/tmp/sample1.txt"
tmpFile2="/tmp/sample2.txt"
TOTAL_FILEDS=27
COUNT=1
rm -f $outFile
for line in $(cat $inpFile)
do
        count_fields=$(echo $line | sed "s/00,*//g;s/,$//g" | wc -c)
        cnt_flds1=$(echo $line | cut -d',' -f1-9 | sed "s/00,*//g;s/,$//g" | wc -c )
        cnt_flds2=$(echo $line | cut -d',' -f10-18 | sed "s/00,*//g;s/,$//g" | wc -c )
        cnt_flds3=$(echo $line | cut -d',' -f19-27 | sed "s/00,*//g;s/,$//g" | wc -c )
        if [[ $count_fields -eq 45 && $cnt_flds1 -eq 15 && $cnt_flds2 -eq 15 && $cnt_flds3 -eq 15 ]]
        then
                echo $line >> $outFile
        fi
done
if [[ ! -s $outFile ]]
then
        echo "No output file is created!!! Check your input file"
        exit 126
fi
while [[ $COUNT -le $TOTAL_FILEDS ]]
do
        cut -d',' -f$COUNT $outFile | grep -v 00 > $tmpFile1
        cut -d',' -f$COUNT $outFile | grep -v 00 | sort > $tmpFile2
        diff $tmpFile1 $tmpFile2 > /dev/null
        if [[ $? -eq 1 || ! -s $tmpFile1 ]]
        then
                echo "Check the $COUNT field of the INPUT FILE"
                exit 256
        fi
        COUNT=$((COUNT+1))
done
echo "Success.... Check the output in $outFile"
rm -f $tmpFile1 $tmpFile2

Input file

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

lorcan,
thanks for the input.

when i ran the script it is telling

./testingscript testingdata
Check the 1 field of the INPUT FILE

it is not incrementing the COUNT value so it is printing only a single record at a time.

You mentioned ---
This script checks for the 15 tickets in a row and also checks if there are 5 tickets within every 9 columns.

And as a whole i am checking whether there are atleast one ticket in a column and arranged in ascending order.

The first one you mentioned is perfectly fine.

the second line is somewhat 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

this is one data in a file,it consists of 3 rows and 9 columns.we can break
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

We need to check in this that each column should have min 1 value and if more than that then it should be in ascending order not as a whole.

Thanks for your kind help.