Tricky Shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Tricky Shell script
# 22  
Old 08-14-2007
Namish-

As per your requirement the first column of your input file is not in ascending order due to which the script is throwing an error

Code:
00
02
00
00
06
00
00
00
05
00
04
01
06

# 23  
Old 08-14-2007
CPU & Memory

lorcan,
still the same problem is coming,its not going to the COUNT expression;

is this syntax correct;
while [[ $COUNT -le $TOTAL_FIELDS ]]
do
echo $?
cut -d',' -f1-9 $outFile | grep -v 00 | sort > $tmpFile1
echo $?
cut -d',' -f10-18 $outFile | grep -v 00 | sort > $tmpFile2
echo $?
cut -d',' -f19-27 $outFile | grep -v 00 | sort > $tmpFile3
echo $?
cat tmpFile1 tmpFile2 tmpFile3 > sortFile
echo $?

In the script itself these lines are there i just cut and pasted that.when i am running the script :
cat: cannot open tmpFile1
cat: cannot open tmpFile2
cat: cannot open tmpFile3
this error is coming.

I am trying to track them but its is hard for me.Can you help me out in this.
# 24  
Old 08-14-2007
Error

Quote:
Originally Posted by lorcan
Namish-

As per your requirement the first column of your input file is not in ascending order due to which the script is throwing an error

Code:
00
02
00
00
06
00
00
00
05
00
04
01
06

What can be the problem for this because as for as i know the syntax are fine.Why this problem is coming?
# 25  
Old 08-14-2007
I'm not sure I understand the requirements, but if I do, maybe this will do it:
Code:
#! /usr/bin/ksh

exec < data
lineno=0

#
# read input line and break it into data items in an array

while read line ; do
        ((lineno=lineno+1))
        oIFS="$IFS"
        IFS=,
        set -A fields  $line
        IFS="$oIFS"

#
# we want 27 data items

        if ((${#fields[*]} != 27)) ; then
                echo error line $lineno has ${#fields[*]} data items:
                echo $line
                echo
                continue
        fi

#
# scan the 27 data items to make sure they are numbers
# and remove leading zeros while we are at it
        nitem=0
        error=0
        while ((nitem < 27)) ; do
                if [[ ${fields[nitem]} != [0-9][0-9] ]] ; then
                        echo error line $lineno has non numeric data:
                        echo $line
                        echo
                        error=1
                        break
                fi
                if [[ ${fields[nitem]} = 0[0-9] ]] ; then
                        fields[nitem]=" ${fields[nitem]#0}"
                fi
                ((nitem=nitem+1))
        done
        ((error)) && continue
#
#  the 27 numbers are really 3 tickets

        nitem=0
        while ((nitem < 9)) ; do
                ticket1[nitem]="${fields[nitem]}"
                ticket2[nitem]="${fields[nitem+9]}"
                ticket3[nitem]="${fields[nitem+18]}"
                ((nitem=nitem+1))
        done

#
# each ticket must have exactly 5 non-zeros and while we check that,
# note each column with a non-zero
        nonzero1=0
        nonzero2=0
        nonzero3=0
        nitem=0
        set -A cols 0 0 0 0 0 0 0 0 0
        while ((nitem < 9)) ; do
                ((${ticket1[nitem]})) && ((cols[nitem]=1)) && ((nonzero1=nonzero1+1))
                ((${ticket2[nitem]})) && ((cols[nitem]=1)) && ((nonzero2=nonzero2+1))
                ((${ticket3[nitem]})) && ((cols[nitem]=1)) && ((nonzero3=nonzero3+1))
                ((nitem=nitem+1))
        done

        if ((nonzero1 != 5 || nonzero2 != 5 || nonzero3 != 5)) ; then
                echo error line $lineno does not have 3 tickets each with 5 non-zeros: $nonzero1 $nonzero2 $nonzero3
                echo $line
                echo
                break
        fi

#
# now scan the cols array to be sure we had a non-zero in each column
        nitem=0
        error=0
        while ((nitem < 9)) ; do
                if [[ ${cols[item]} = 0 ]] ; then
                        echo error line $lineno does not have a ticket with a nonzero in column ${nitem}:
                        echo $line
                        echo
                        error=1
                        break
                fi
                ((nitem=nitem+1))
        done
        ((error)) && continue

#
# check for ascending columns

        nitem=0
        error=0
        set -A cols 0 0 0 0 0 0 0 0 0
        while ((nitem < 9)) ; do
                if ((${ticket1[nitem]})) ; then
                        cols[nitem]=${ticket1[nitem]}
                fi
                if ((${ticket2[nitem]})) ; then
                        ((${ticket2[nitem]} < ${cols[nitem]})) && error=1
                        cols[nitem]=${ticket2[nitem]}
                fi
                if ((${ticket3[nitem]})) ; then
                        ((${ticket3[nitem]} < ${cols[nitem]})) && error=1
                fi
                ((nitem=nitem+1))
        done
        if ((error)) ; then
                echo error line $lineno does not have ascending columns:
                echo $line
                echo "${ticket1[*]}"
                echo "${ticket2[*]}"
                echo "${ticket3[*]}"
                echo
        fi

done
exit 0

# 26  
Old 08-16-2007
MySQL

perdarabo,
Thannks for the input,that helped me in getting different ideas to solve that problem.The requirement is a bit different but now i will be able to solve that.

Thanks
Namish
# 27  
Old 08-16-2007
MySQL

One more thing i need to do in this that 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

This is one data in file.
i need to break this in three lines in a set of nine ie

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

I have to check that the data is in ascending order in all the columns.

Thanks for all your kind help.
# 28  
Old 08-16-2007
Quote:
Originally Posted by namishtiwari

I have to check that the data is in ascending order in all the columns.
My script already checks for that..if I understand what you mean...

Code:
#
# check for ascending columns

        nitem=0
        error=0
        set -A cols 0 0 0 0 0 0 0 0 0
        while ((nitem < 9)) ; do
                if ((${ticket1[nitem]})) ; then
                        cols[nitem]=${ticket1[nitem]}
                fi
                if ((${ticket2[nitem]})) ; then
                        ((${ticket2[nitem]} < ${cols[nitem]})) && error=1
                        cols[nitem]=${ticket2[nitem]}
                fi
                if ((${ticket3[nitem]})) ; then
                        ((${ticket3[nitem]} < ${cols[nitem]})) && error=1
                fi
                ((nitem=nitem+1))
        done
        if ((error)) ; then
                echo error line $lineno does not have ascending columns:
                echo $line
                echo "${ticket1[*]}"
                echo "${ticket2[*]}"
                echo "${ticket3[*]}"
                echo
        fi

Note that I explicitly ignore zeros during this check...only non-zeros are required to ascend in columns. I think that is what you wanted.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing xml - tricky one...need help!!

Hi, i am new to linux programming fraternity but looks like starting with a big thing... yes..xml parsing (it is indeed tough for a beginner like me) so need your kind help... The snippet of xml looks like: <snapshot> <tag1> <key>1234</key> <keytype>abcd</keytype> </tag1> <tag2>... (11 Replies)
Discussion started by: rookie2014
11 Replies

2. Shell Programming and Scripting

Tricky sed required

Hi All I need to put some sed together for a task and its a bit advanced for me, so I thought I'd ask if anyone here could help. I have a csv file with content like this - "","abcde","","" "'","abcde","","" "","","","1234" "'e'","","","" I need to remove any single quotes that fall... (17 Replies)
Discussion started by: steadyonabix
17 Replies

3. Solaris

Tricky egrep

Hi folks! My first post here. I'm working on a script that retrieves a range of files from a list depending on a range of time. UPDATE: I've seen it could be difficult to read all this thing, so I'll make a summarize it.. How come I do this and take a result.. grep "..\:.." lista.new |... (4 Replies)
Discussion started by: kl0x
4 Replies

4. Shell Programming and Scripting

Tricky data manipulation...

Hi everyone.. I am new here, hello.. I hope this doesn't come across to you folks as a stupid question, I'm somewhat new to scripting :) I'm seeking some help in finding a way to manipulate data output for every two characters - example: numbers.lst contains the following output:... (3 Replies)
Discussion started by: explicit
3 Replies

5. Shell Programming and Scripting

Tricky - Need help on Shell script variables

Hi, I have a requirement in which i have to read a csv file and put data in certain set of variables: File content: VP-DTL-REC-CNT, ,854840,0.00,VP-PAID-AMT, ,0,32280885.17,VP-PAT-PAID-AMT, ,0,9930244.32,VP-PAID-REV-CNT, ,484927,0.00,VP-REJ-CNT, ,369913,0.00, , ,0,0.00, , ,0,0.00, , ,0,0.00, ,... (3 Replies)
Discussion started by: shantoshkumar
3 Replies

6. Shell Programming and Scripting

Linux: Writing a tricky script to check connectivity

So, first and foremost, I'm having issues with my internet connection. Periodically, the connection drops across the network. The fix is simple enough: restart the modem. However, this gets old when the connection dies out every hour. I can hit my surfboard on 192.168.100.1, and navigate to a... (5 Replies)
Discussion started by: kungfujoe
5 Replies

7. UNIX for Dummies Questions & Answers

Tricky Quotation Question

Hi, I am at a point in my script where I defined the number of the command line parameter I would like to set a variable equal to: parameter_number=14 I would then like to set a variable equal to the correct parameter: variable=$parameter_number The issue here is that {} is required... (2 Replies)
Discussion started by: msb65
2 Replies

8. Shell Programming and Scripting

Tricky script question

Hi, I'm in the midst of writing a UNIX script that sftp's files to an external host and am stuck with a problem. The problem is that the files created on my server as a order number that correlates to a sequence of directories on the remote host which is where the file should be ftp'ed. ... (3 Replies)
Discussion started by: budrito
3 Replies

9. Shell Programming and Scripting

Tricky Sed

Hello. I am trying to convert occurrences of 'NULL' from a datafile. The 'NULL' occurences appears at this: |NULL| NULL|NULL| NULL|NULL| NULL|NULL| NULL| There should be 52 fields per line. I would like any occurrence of | NULL| or |NULL| to appear as '||' Currently I am using this sed... (2 Replies)
Discussion started by: bestbuyernc
2 Replies

10. Windows & DOS: Issues & Discussions

Tricky one...

Here's my problem: I have a laptop running Windows XP Pro with no internal CD or Floppy drives. I want to install Linux on it. I don't care about the Windows XP Pro installation, in fact I would like to install Linux over the entirety of the HD. However I cannot boot from any external CD drive... (1 Reply)
Discussion started by: saabir
1 Replies
Login or Register to Ask a Question