Tricky Shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Tricky Shell script
# 8  
Old 08-11-2007
please use the provided code tags for code.
# 9  
Old 08-11-2007
i could get only the requirement 3

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

Code:
#!/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


Last edited by lorcan; 08-11-2007 at 10:14 AM.. Reason: changed the pattern in sed
# 10  
Old 08-11-2007
CPU & Memory

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.

Last edited by namishtiwari; 08-11-2007 at 09:45 AM..
# 11  
Old 08-11-2007
Havent tested it, You can give a try

Code:
#!/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

# 12  
Old 08-13-2007
MySQL

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
# 13  
Old 08-13-2007
Might be the output file is not created based on the condition in the if loop.
Can you run the script in debug mode
# 14  
Old 08-13-2007
MySQL

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