string manipulating


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers string manipulating
# 1  
Old 04-15-2008
Tools 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 05:21 AM.. Reason: added code tags
# 2  
Old 04-15-2008
You could use something like this:

Code:
sum=$(($(cut -d\; -f2 file|paste -sd+ -)))

# 3  
Old 04-15-2008
Tools 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  
Old 04-15-2008
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  
Old 04-15-2008
Tools 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  
Old 04-15-2008
...

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

# 7  
Old 04-15-2008
Tools i do this but

I am giving th commands not from shell but within the script. When it enters while it stops and when a press a key it only prints the body o f while. It doesnt execute nothing
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Korn Shell manipulating the string into dynamic currency number

Conversion of string into currency value.. ex1: number_of_positions=2 input_string=345987 Output= 345,987.00 ex2: number_of_positions=4 input_string=1345987 Output= 1,345,987.0000 Please respond as soon as possible edit by bakunin: we will gladly respond as soon as... (15 Replies)
Discussion started by: suren.bills
15 Replies

2. Shell Programming and Scripting

Manipulating Filenames

Hi Folks, I'm looking for some ideas on how to change some file names. I'm pretty sure I need to use sed or awk but they still escape me. The files I have are like: VOD0615 NEW Blades R77307.pdf or VOD0615_NEW_Blades_R77307.pdf and what I want after processing is: R77307 NEW Blades.pdf ... (5 Replies)
Discussion started by: imonkey
5 Replies

3. Shell Programming and Scripting

Need help in manipulating string variable

I have written a shell script to do some processing and have to manipulate a variable. Basically, the variable is like this -- var=set policy:set cli My purpose is to split it into two variables based on the position of ":". To get the right end, I am doing this -- vaa1=${vaa#*:} ... (1 Reply)
Discussion started by: Dev_Sharma987
1 Replies

4. Shell Programming and Scripting

Manipulating a file

Hi everybody, I need an urgent help with a BASH script. I have file which contains (besides the other data) the lines with the following structure identified by with keyword PCList: <PARAMETER NAME="PCList" TYPE="LIST_STRUCTURE" MODEL="{,}" ... (1 Reply)
Discussion started by: sameucho
1 Replies

5. Solaris

Manipulating TOP

I need the command top to output as: Memory: 2048M real, 1499M free, 53M swap in use, 5423M swap free on just the memory line. Instead, I have compiled the new version of top that displays as so: Memory: 2048M phys mem, 1499M free mem, 5476 total swap, 5423M swap free I read... (2 Replies)
Discussion started by: adelsin
2 Replies

6. Shell Programming and Scripting

manipulating data

Hi guys Firstly, I'd like to say hi and how great this forum is. I'm not new to UNIX but am relatively new to scripting. I have a personal project that I'm working on just to try and speed up my learning. I working with a text file, well more of a logfile really. It has several columns of... (6 Replies)
Discussion started by: abcd69
6 Replies

7. Emergency UNIX and Linux Support

Manipulating Data

Hi. I haven't had to write bash scripts in a long time and have a simple task to do, but need some help: Input: chrY:22627291-22651542 chrY:23045932-23070172 chrY:23684890-23696359 chrY:25318610-25330083 chrY:25451096-25462570 chr10:1054847-1061799 chr10:1058606-1080131... (7 Replies)
Discussion started by: awknerd
7 Replies

8. Shell Programming and Scripting

retreiving and assigning values and manipulating string in a for loop

Hi I am new to shell scripting and i am preparing a script. for now i am work on a sub part of it..but i am unable to make it work. --- the test code that i am working on -------------------------- IFS="" Sample_eve=`psg proc_s | grep tY` n=0 for line in $Sample_eve... (41 Replies)
Discussion started by: Anteus
41 Replies

9. UNIX for Dummies Questions & Answers

Help!! manipulating file

Hi all, I need help manipulating the file below. Here is what I needed to do. First, I have to replace INSUPD to DELETE. Then I need to change the content of the file around by flipping the contents in the file from the bottom to the top (start from "CMD") How should I attack this? Here... (2 Replies)
Discussion started by: sirrtuan
2 Replies

10. UNIX for Advanced & Expert Users

Manipulating two files

Hi Friends, I prefer to represent my problem with example. I have two files as below: file1.txt --------- abcd.....1234......XY abcd.....1235......XX abcd................. abcd...231236..1111YX abcd...241236..1112YY abcd...241237......YY abce.....1235......YY file2.txt ------- ... (4 Replies)
Discussion started by: rinku11
4 Replies
Login or Register to Ask a Question