Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Aug 2007
    Posts
    21

    Thumbs up Unanswered: Tricky Shell script

    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

  2. #2
    Join Date
    Aug 2007
    Posts
    21

    Thumbs up

    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
    done
    echo " We have completed the task,at the end of program"


    I am using solaris sytem
    Last edited by namishtiwari; 08-11-07 at 09:44.

  3. #3
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    namishtiwari

    You must keep to the same thread - starting a new thread only confuses people and will annoy the moderators.

    I'm afraid your description of the problem is quite poor - I've read each of your three threads and still have no idea how your data is laid out or what you're trying to do with it.

    I'd suggest you do as georgev suggested and run your original sql script into the database and process it within the database. It may be difficult to check the rows are in the correct sequence using this method though.

    Mike

  4. #4
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    Quote Originally Posted by mike_bike_kite
    I'm afraid your description of the problem is quite poor - I've read each of your three threads and still have no idea how your data is laid out or what you're trying to do with it.
    I thought I'm the only one who didn't "get it".
    Knut Stolze
    IBM DB2 Analytics Accelerator
    IBM Germany Research & Development

  5. #5
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    I thought I'm the only one who didn't "get it".
    Well - I guess that's you, me and the OP then

  6. #6
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    There are erros when i ran the script but its hard for me to track them.
    What error messages are being returned?
    I notice you are using echo lines to return information to the user...
    Can you tell us which of these lines are returned before you receive the error message(s)?

    this should help you narrow down where the error is occuring.
    George
    Home | Blog

  7. #7
    Join Date
    Aug 2007
    Posts
    21

    Thumbs up

    With some guidance i wrote this script in Ksh

    #!/bin/ksh

    inpFile=$1
    outFile="$HOME/Output.txt"
    echo "The out put file is $outFile"
    modFile="$HOME/mod_inputfile.txt"
    tmpFile1="$HOME/sample1.txt"
    tmpFile2="$HOME/sample2.txt"

    TOTAL_FILEDS=9
    COUNT=1
    #rm -f $outFile
    sed 's/ /,/g' $inpFile > $modFile
    echo " The file modified is $modFile"
    for line in `cat $modFile`
    do
    count_fields=$(echo $line | sed "s/00,*//g;s/,$//g" | wc -c)
    if [[ $count_fields -eq 45 ]]
    then
    echo $line >> $outFile
    else
    echo "There are more than 15 numbers in a line"
    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

    The InputFile i used 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
    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

    ,its working but it is not incrementing the value of count,that is why it is not going to next column.
    Can anyone suggest to rectify that.

  8. #8
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    your second post uses this
    Code:
    count = `expr $count + 1`
    and the new one uses
    Code:
    COUNT=$((COUNT+1))
    Does the first one work?
    George
    Home | Blog

  9. #9
    Join Date
    Aug 2007
    Posts
    21

    Arrow

    Georgev,
    i wrote that script in ksh,
    no the first one did not work.Can you help me out for the problem.

    Thanks

  10. #10
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Try this:
    Code:
    COUNT=$(($COUNT+1))
    George
    Home | Blog

  11. #11
    Join Date
    Aug 2007
    Posts
    21

    Thumbs up

    George,
    Its the same output,the change has no effect on the output..

    The output is ----
    ./script_testing_new datafile
    The out put file is /home/NamishT/Output.txt
    The file modified is /home/NamishT/mod_inputfile.txt
    Check the 1 field of the INPUT FILE
    ,it is not going to the count field at all.
    when i print the exit command status before line
    if [[ $? -eq 1 || ! -s $tmpFile1 ]]
    it goes to the echo "Success.... Check the output in $outFile"
    but does not show anything in the sample1.txt and sample2.txt

  12. #12
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Try running this
    Code:
    count = 1
    while [ $count -gt 10 ] ; then
    	echo "$count"
    	count=$(( $count + 1 ))
    done
    exit 0
    I think the syntax is right (I know very little USS )
    George
    Home | Blog

  13. #13
    Join Date
    Aug 2007
    Posts
    21

    Arrow

    George,
    That syntax is also right.I checked that on command line itself and then i put in the shell script.
    I am not able to debug the problem why it is showing this.

  14. #14
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    What is the value of "TOTAL_FILEDS"
    Is Ksh really anal about white space?
    Example:
    Code:
    count=$(($count+1))
    count=$(( $count + 1 ))
    Do the spaces make any different to the parser?
    I'm now stabbing in the dark
    George
    Home | Blog

  15. #15
    Join Date
    Aug 2007
    Posts
    21

    Lightbulb

    The value of TOTAL_FILEDS is 27.
    Yes all shells are anal about white spaces so we need to be carefull while handling.

    The $ inside the open brackets is not required in ksh and spaces also before and after + sign.

Posting Permissions

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