UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 16th, 2005, 01:07 AM
stevefox stevefox is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 12 stevefox User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 42 m 29 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old November 16th, 2005, 10:42 AM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,308 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 5 h 20 m 41 sec
Reputation Power: 48
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 

Reply With Quote
  #3  
Old November 17th, 2005, 03:08 AM
stevefox stevefox is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 12 stevefox User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 42 m 29 sec
Reputation Power: 0
Question

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.

Reply With Quote
  #4  
Old January 14th, 2006, 05:53 PM
Ehlanna's Avatar
Ehlanna Ehlanna is offline
Not a clue what to put ...
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2006
Location: in front of this keyboard
Posts: 815 Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 5 h 43 m 48 sec
Reputation Power: 243
Have a look at the tr command (I think it will be tr -s " ").

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Shell help needed


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT