The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Manipulating File rivendell500 SUN Solaris 2 03-25-2008 10:52 PM
manipulating csv to add extra row neil546 Shell Programming and Scripting 3 07-16-2007 07:11 AM
Manipulating two files rinku11 UNIX for Advanced & Expert Users 4 12-11-2006 03:47 AM
Manipulating awk $variables using sed? r0sc0 Shell Programming and Scripting 2 04-04-2006 06:53 PM
date manipulating tamer UNIX for Dummies Questions & Answers 3 01-08-2001 09:46 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-15-2008
psalas psalas is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 19
Cool string manipulating

I have a file like this:
Code:
Tue Apr 15 10:41:47 MDT 2008 FINAL RESULT; 6
Tue Apr 15 10:41:47 MDT 2008 FINAL RESULT; 2
Tue Apr 15 10:41:47 MDT 2008 FINAL RESULT; 5
With this command seira=`cut -f 2 -d ';' tes.txt` i take all the results (6,2,5 etc) and i store them in variable seira

When i do echo $seira it prints: 6 2 5

With this command i want to take the sum of the results from seira where they are stored 6+2+5 etc

Code:
while read seira
	do
	echo $seira
		sum=0
		sum='expr $sum + $seira'
		echo $sum
	done
But it doesn't work. What is wrong?

Last edited by Yogesh Sawant; 04-15-2008 at 04:21 AM.. Reason: added code tags
  #2 (permalink)  
Old 04-15-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
You could use something like this:

Code:
sum=$(($(cut -d\; -f2 file|paste -sd+ -)))
  #3 (permalink)  
Old 04-15-2008
psalas psalas is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 19
Cool Ok But

Thank you but this is my last option. I would like an option with while an d the read command to learn better manipulate strings.Thats why. Can you or anybody else suggest anything else? Withe while read?
  #4 (permalink)  
Old 04-15-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Code:
$ while read;do num=$((num+${REPLY##*;}));done<file;printf "%d\n" $num
13
With zsh:

Code:
% while read;do ((num+=${REPLY##*;}));done<file;print $num
13
  #5 (permalink)  
Old 04-15-2008
psalas psalas is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 19
Cool not exactly

something is not working could someone fix the problem with this solution:
while read seira
do
sum=0
sum='expr $sum + $seira'
echo $sum
done
  #6 (permalink)  
Old 04-15-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
...

Code:
cut -d\; -f2 file|while read seira
do
  sum=`expr ${sum-0} + $seira`
  echo $sum
done
Closed Thread

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0