|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
contents of filename:
Code:
AAA Unit1 60 39 AAA Unit7 30 15 BBB Unit3 80 60 CCC Unit4 50 25 CCC Unit8 90 45 output Code:
AAA 0.60 CCC 0.50 script Code:
awk '
BEGIN{ one="";
three="";
four="";}
{ if (one==$1 )
{
printf("%s %.2f\n",$1, (four + $4)/(three + $3) );
}
one=$1
three=$3
four=$4
} ' filename
|
|
#3
|
|||
|
|||
|
Thanks Jim!
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 where @ represents single space. |
|
#4
|
||||
|
||||
|
Have a look at the tr command (I think it will be tr -s " ").
|
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Shell help needed |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|