Shell help needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell help needed
# 1  
Old 11-16-2005
Question Shell help needed

Could someone tell me how to do the below in Korn Shell or SED?

If the 1st word (i.e. 1st character to the one character before the 1st space) of a line is the same as the 1st word of the 2nd line then add the 3rd word of the 1st line and the 3rd word of the 2nd line and divide the sum of the 4th word of the 1st and 2nd line and put the result into a new file with only the 1st word and result of the division. And I want this to loop until it reachs the end of the file.

e.g. I have a file which contains 5 lines below:

AAA Unit1 60 39
AAA Unit7 30 15
BBB Unit3 80 60
CCC Unit4 50 25
CCC Unit8 90 45

I want it to output 3 lines below:

AAA 0.6
BBB 0.75
CCC 0.5


0.6 was calculated by (39+15)/(60+30) taken from 1st and second line.


Any help will be greatly appreciated.
# 2  
Old 11-16-2005
Change the finename in the below script and test

Code:
#!/usr/bin/ksh
print "\c" > sum_divide_final
for i in `cut -f1 -d" " sum_divide.dat | uniq`
do
dividend=0
divider=0
        for j in `grep $i sum_divide.dat | cut -f3 -d" "`
        do
                (( divider = divider + j ))
        done
        for j in `grep $i sum_divide.dat | cut -f4 -d" "`
        do
                (( dividend = dividend + j ))
        done
finalresult=$(print "scale = 2; $dividend/$divider" | bc)
print "$i"" ""$finalresult" >> sum_divide_final
done


awk soln


Code:
BEGIN{
IFS=" "
OFS=" "
}
{
arr3[$1]+=$3
arr4[$1]+=$4
}
END{
for(item in arr3)
{
        arr5[item]=arr4[item]/arr3[item]
}
for(item in arr5)
{
        print item " " arr5[item]
}
}

# 3  
Old 11-16-2005
try this one,

not thoroughly tested;

Code:
#!/usr/bin/ksh

prev="###"
sum1=0
sum2=0
while read i
do
curr=`echo $i | cut -f1 -d" "`
val1=`echo $i | cut -f3 -d" "`
val2=`echo $i | cut -f4 -d" "`

if [ \( $curr != $prev \) -a \( $prev != "###" \) ]
then
final=$(print "scale = 2; $sum2/$sum1" | bc)
echo $prev $final
sum1=0
sum2=0
fi

sum1=$(($sum1 + $val1))
sum2=$(($sum2 + $val2))

prev=$curr
done < input.file > output.file

final=$(print "scale = 2; $sum2/$sum1" | bc)
echo $prev $final >> output.file

exit 0

# 4  
Old 11-16-2005
When i tried the awk solution,
i got the following error.. pls explain

awk: awk.sh1:14: (FILENAME=ifile3 FNR=6) fatal: division by zero attempted
# 5  
Old 11-16-2005
Awk:
Code:
{ if ( $1 == old_word )
  { split( old_line, a )
    print $1, (a[4]+$4)/(a[3]+$3)
    old_word = old_line = ""
  }
  else
  { if ( old_line )
    { split ( old_line, a)
      print a[1], a[4]/a[3]
    }
    old_word = $1; old_line = $0
  }
}

# 6  
Old 11-17-2005
Question

Thanks everyone!
I haven't tested all of them yet but I have a question with Mona's code which I tested.
The code worked perfectly but I was wondering if the 2nd line of the code is necessary or not.

print "\c" > sum_divide_final

I guess this line is creating an empty file but it worked fine without this line (I think because it created it in the 2nd last line).
# 7  
Old 11-17-2005
Question

In case of when I have an input file which has more than one space between each word and I want the previous program to work for those type of input.

Could someone tell me how to make all spaces between each words to be one space by using KSH or SED commands?

e.g.
I have a file which contains 3 lines below:
AAA Unit1 60
BBB Unit3 80
CCC Unit4 50

I want it to have only one space between each word like below:
AAA Unit1 60
BBB Unit3 80
CCC Unit4 50
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script - help needed

I want to take out the Z1 value from the lscfg outpu and use the below command to get it lscfg -vl hdisk0 | grep "Device Specific.(Z1)" | awk -F. '{print $NF}' # lscfg -vpl hdisk0 . . Device Specific.(Z0)........0000063268181002 Device Specific.(Z1)........020064a . And it works,... (2 Replies)
Discussion started by: moorthikv
2 Replies

2. Shell Programming and Scripting

Help needed with shell script

Hi, I have trouble building the logic for the following, could anyone please help me out with this: Im working on a wrapper script to check for input files (in a specific directory) and pass the input files along with the options as parameters to the main script.The options vary depending ... (2 Replies)
Discussion started by: stunnerz_84
2 Replies

3. Shell Programming and Scripting

Help needed with Shell scripting

Hi All, I need to split a flatfile based on it's first character, I am using the following script awk '{print > "TEST_substr($0,1,1).txt"}' PROVIDER.txt It is returning files TEST_1 and TEST_2 But I am not getting the .txt file extension. I need the files like TEST_1.txt and ... (1 Reply)
Discussion started by: sam35
1 Replies

4. UNIX for Dummies Questions & Answers

Help needed with Shell script

Hi I want a script which should basically do 1. If the size of the file is 0kb, send email to some list od ppl 2. if the size of the file is other than 0kb send email to someother list... Pls help (2 Replies)
Discussion started by: win4luv
2 Replies

5. Shell Programming and Scripting

Shell script help is needed

I have a file test.txt and i need to grep pattern "A.17" from that file. I know cat test.txt | grep A.17 will return the pattern, but it is returing like # VERSION=A.17 How can i take only A.17 from this if A.17 is found, ... do something if not found ... do something Please... (11 Replies)
Discussion started by: Renjesh
11 Replies

6. Shell Programming and Scripting

help needed for a shell script

i need to search the starting line example we have -sh shl-js-gd i need to search only starting -sh not the other i have used cmd cat filename | grep '-' but it will check for complete - in the file please help me to search only starting - thank u revenna (0 Replies)
Discussion started by: revenna
0 Replies

7. Shell Programming and Scripting

shell script help needed

I am trying to query a table having 3 columns, the third column is a field of varchar(1024) with a SQL string in it. I am using cut command to split out the three fields into three variables. I do a db2 command to extract the data into a file. My problem is with the third field having the SQL... (3 Replies)
Discussion started by: fastgoon
3 Replies

8. Shell Programming and Scripting

SHell Scripting Help Needed

Dear All, I have an input file like this interface Serial10/0/7:11.1 point-to-point description CLIENT:SA_INSTITUTO ANGLO MEXICANO Sitio Metepec 104452:0,165 bandwidth 64 ip vrf forwarding INSTITUTO-ANGLO ip address 192.168.148.217 255.255.255.252 no ip directed-broadcast frame-relay... (2 Replies)
Discussion started by: cskumar
2 Replies

9. Shell Programming and Scripting

K Shell Help needed

Could someone tell me the code for doing the below inside a k shell? I have a file file below: $ more file1 >>>>> AAA BBB CCC <<<<< >>>>> DDD EEE FFF <<<<< I want the lines between ">>>>>" and "<<<<<" to be one line like below: AAA BBB CCC (2 Replies)
Discussion started by: stevefox
2 Replies

10. Shell Programming and Scripting

Korn Shell Help Needed

How do I change directories to a path given by input variable in Korn Shell? e.g. I tired with the Korn Shell below but it doesn't work. ---------------------------------- #!/bin/ksh echo "Enter folder name: \c" read folder cd $folder ---------------------------------- Any help will... (5 Replies)
Discussion started by: stevefox
5 Replies
Login or Register to Ask a Question