| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |
|

08-11-07, 05:30
|
|
Registered User
|
|
Join Date: Aug 2007
Posts: 21
|
|
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
|
|

08-11-07, 07:46
|
|
Registered User
|
|
Join Date: Aug 2007
Posts: 21
|
|
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 08:44.
|

08-12-07, 15:24
|
|
vaguely human
|
|
Join Date: Jun 2007
Location: London
Posts: 2,519
|
|
|
|
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
|
|

08-12-07, 16:42
|
|
Registered User
|
|
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
|
|
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
|
|

08-13-07, 04:26
|
|
vaguely human
|
|
Join Date: Jun 2007
Location: London
Posts: 2,519
|
|
Quote:
|
I thought I'm the only one who didn't "get it".
|
Well - I guess that's you, me and the OP then 
|
|

08-13-07, 05:16
|
|
www.gvee.co.uk
|
|
Join Date: Jan 2007
Location: UK
Posts: 10,156
|
|
Quote:
|
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.
|
|

08-13-07, 06:20
|
|
Registered User
|
|
Join Date: Aug 2007
Posts: 21
|
|
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.
|
|

08-13-07, 06:25
|
|
www.gvee.co.uk
|
|
Join Date: Jan 2007
Location: UK
Posts: 10,156
|
|
your second post uses this
Code:
count = `expr $count + 1`
and the new one uses
Does the first one work?
|
|

08-13-07, 06:33
|
|
Registered User
|
|
Join Date: Aug 2007
Posts: 21
|
|
Georgev,
i wrote that script in ksh,
no the first one did not work.Can you help me out for the problem.
Thanks
|
|

08-13-07, 06:38
|
|
www.gvee.co.uk
|
|
Join Date: Jan 2007
Location: UK
Posts: 10,156
|
|
Try this:
Code:
COUNT=$(($COUNT+1))
|
|

08-13-07, 06:46
|
|
Registered User
|
|
Join Date: Aug 2007
Posts: 21
|
|
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
|
|

08-13-07, 06:52
|
|
www.gvee.co.uk
|
|
Join Date: Jan 2007
Location: UK
Posts: 10,156
|
|
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  )
|
|

08-13-07, 07:00
|
|
Registered User
|
|
Join Date: Aug 2007
Posts: 21
|
|
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.
|
|

08-13-07, 07:12
|
|
www.gvee.co.uk
|
|
Join Date: Jan 2007
Location: UK
Posts: 10,156
|
|
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 
|
|

08-13-07, 07:23
|
|
Registered User
|
|
Join Date: Aug 2007
Posts: 21
|
|
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.
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|