Tricky Shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Tricky Shell script
# 36  
Old 08-16-2007
CPU & Memory

One thing come in my mind if
the data is not in ascending order in columns then
we should through some errors that "this data is not in ascending order"

Instead of sorting that we should check that the data is in ascending order or not.Smilie
# 37  
Old 08-16-2007
Error

Quote:
Originally Posted by lorcan
Code:
#!/bin/ksh

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

tmpFile1="/tmp/sample1.txt"
tmpFile2="/tmp/sample2.txt"
sortFile="/tmp/sortfile.txt"

TOTAL_FILEDS=9
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 | sed "s/\(.\{26\}\),\(.\{26\}\),\(.\{26\}\).*/\1\n\2\n\3/g" > $sortFile
                while [[ $COUNT -le $TOTAL_FILEDS ]]
                do
                        cut -d',' -f$COUNT $sortFile | grep -v 00 > $tmpFile1
                        cut -d',' -f$COUNT $sortFile | grep -v 00 | sort > $tmpFile2
                        diff $tmpFile1 $tmpFile2 > /dev/null
                        if [[ $? -eq 1 || ! -s $tmpFile1 ]]
                        then
                                echo "Check the $COUNT field of the line: $line"
                                rm -f $tmpFile1 $tmpFile2 $sortFile
                                exit 256
                        fi
                COUNT=$((COUNT+1))
                done
                echo $line # >> $outFile
        fi
        COUNT=1
done
rm -f $tmpFile1 $tmpFile2 $sortFile
exit 0

Hope your req is complete Smilie
I like to know what this line is doing:

echo $line | sed "s/\(.\{26\}\),\(.\{26\}\),\(.\{26\}\).*/\1\n\2\n\3/g" > $sortFile
# 38  
Old 08-16-2007
Error

when i ran the script it tells

./newfinalscript testingdata
Check the 1 field of the line: 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

The data is not getting sort in that because of that this is coming.
# 39  
Old 08-16-2007
It is not throwing me any error. I have added this line to the file and the script works fine

Code:
$ >ksh nam.ksh input2
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
$ >echo $?
0

And moreover the data is sorted in this line, so it should not throw error

Code:
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

# 40  
Old 08-16-2007
CPU & Memory

No for me it still throws the same error ie--
ksh newfinalscript testingdata
Check the 1 field of the line: 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
it is again not sorting the whole but taking only one record that means not incrementing the COUNT value.

OK,Now i need to check in the testingdata file if the columns are not in ascending order (The format is exactly like the requirement) then it should through an error that this record is not in ascending order.

While now we are just sorting the data and stroing that data in file not checking for that.
# 41  
Old 08-16-2007
Post the script output of the script when run as a debug mode, like

ksh -x newfinalscript testingdata

Code:
+ inpFile=input2
+ outFile=/home/Output.txt
+ tmpFile1=/tmp/sample1.txt
+ tmpFile2=/tmp/sample2.txt
+ sortFile=/tmp/sortfile.txt
+ TOTAL_FILEDS=9
+ COUNT=1
+ rm -f /home/vrajiah/Output.txt
+ cat input2
+ echo 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
+ sed s/00,*//g;s/,$//g
+ wc -c
+ count_fields=     45
+ echo 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
+ cut -d, -f1-9
+ sed s/00,*//g;s/,$//g
+ wc -c
+ cnt_flds1=     15
+ echo 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
+ cut -d, -f10-18
+ sed s/00,*//g;s/,$//g
+ wc -c
+ cnt_flds2=     15
+ echo 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
+ cut -d, -f19-27
+ sed s/00,*//g;s/,$//g
+ wc -c
+ cnt_flds3=     15
+ echo 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
+ sed s/\(.\{26\}\),\(.\{26\}\),\(.\{26\}\).*/\1\n\2\n\3/g
+ > /tmp/sortfile.txt
+ cut -d, -f1 /tmp/sortfile.txt
+ grep -v 00
+ > /tmp/sample1.txt
+ cut -d, -f1 /tmp/sortfile.txt
+ grep -v 00
+ sort
+ > /tmp/sample2.txt
+ diff /tmp/sample1.txt /tmp/sample2.txt
+ > /dev/null

...

+ COUNT=9
+ cut -d, -f9 /tmp/sortfile.txt
+ grep -v 00
+ > /tmp/sample1.txt
+ cut -d, -f9 /tmp/sortfile.txt
+ grep -v 00
+ sort
+ > /tmp/sample2.txt
+ diff /tmp/sample1.txt /tmp/sample2.txt
+ > /dev/null
+ COUNT=10
+ echo 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
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
+ COUNT=1
+ rm -f /tmp/sample1.txt /tmp/sample2.txt /tmp/sortfile.txt
+ exit 0


Last edited by lorcan; 08-16-2007 at 07:52 AM..
# 42  
Old 08-16-2007
Error

lorcan,
here is the output when i ran in the debug mode-

ksh -x newfinalscript testingdata
+ inpFile=testingdata
+ outFile=/home/NamishT/Outputnew.txt
+ tmpFile1=/tmp/sample1.txt
+ tmpFile2=/tmp/sample2.txt
+ sortFile=/tmp/sortfile.txt
+ TOTAL_FILEDS=9
+ COUNT=1
+ rm -f /home/NamishT/Outputnew.txt
+ cat testingdata
+ + sed s/00,*//g;s/,$//g
+ echo 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
+ wc -c
count_fields= 45
+ + echo 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
+ cut -d, -f1-9
+ sed s/00,*//g;s/,$//g
+ wc -c
cnt_flds1= 15
+ + echo 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
+ cut -d, -f10-18
+ wc -c
+ sed s/00,*//g;s/,$//g
cnt_flds2= 15
+ + echo 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
+ wc -c
+ sed s/00,*//g;s/,$//g
+ cut -d, -f19-27
cnt_flds3= 15
+ [[ 45 -eq 45 ]]
+ [[ 15 -eq 15 ]]
+ [[ 15 -eq 15 ]]
+ [[ 15 -eq 15 ]]
+ echo 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
+ sed s/\(.\{26\}\),\(.\{26\}\),\(.\{26\}\).*/\1\n\2\n\3/g
+ 1> /tmp/sortfile.txt
+ [[ 1 -le 9 ]]
+ cut -d, -f1 /tmp/sortfile.txt
+ grep -v 00
+ 1> /tmp/sample1.txt
+ cut -d, -f1 /tmp/sortfile.txt
+ sort
+ grep -v 00
+ 1> /tmp/sample2.txt
+ diff /tmp/sample1.txt /tmp/sample2.txt
+ 1> /dev/null
+ [[ 0 -eq 1 ]]
+ [[ ! -s /tmp/sample1.txt ]]
+ echo Check the 1 field of the line: 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
Check the 1 field of the line: 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
+ rm -f /tmp/sample1.txt /tmp/sample2.txt /tmp/sortfile.txt
+ exit 256
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