LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-17-2010, 06:09 AM   #1
dynamics
LQ Newbie
 
Registered: Aug 2009
Location: Guildford
Distribution: Ubuntu Jaunty
Posts: 12

Rep: Reputation: 0
shell script for matrix operation


I have 3 files and each contain m*n matrix.
m = number of rows (horizontal lines)
n = number of columns (entries in a particular line)

What I wish to find is the sum of the 2nd number in the last row.
Ex

file1.dat

2 5 8 8
4 6 7 8
3 8 3 7

file2.dat
3 4 1 4
8 4 0 3
4 7 3 7

file3.dat
9 6 3 5
8 4 8 4
7 5 6 8

The desired result is 8+7+5 ie., 20

Thanks in advance for the help. A little bit of explanation about how the script works will be very helpful.
 
Old 09-17-2010, 09:28 AM   #2
jrosco
Member
 
Registered: Aug 2010
Location: Australia
Posts: 37

Rep: Reputation: 4
There might be better way to do this, but what springs to mind would something like

Code:
file1=$(tail -n 1 /tmp/file1.dat); file1=$(echo ${file1:1:1})

file2=$(tail -n 1 /tmp/file2.dat); file2=$(echo ${file2:1:1})

file3=$(tail -n 1 /tmp/file3.dat); file3=$(echo ${file3:1:1})

result=$(expr $file1 + $file2 + $file3)

echo $result
tail -n 1 file #would get the last line of a file

echo ${var:1:1} #would extract characters at position:length var is the string, the first 1 is the position and the second 1 is the length.

Like I said there could be a better way to do this.

JC

Last edited by jrosco; 09-17-2010 at 09:32 AM.
 
1 members found this post helpful.
Old 09-17-2010, 09:43 AM   #3
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Code:
ruby -e 't=0;ARGV.each{|x| t+=IO.readlines(x).last.split[1].to_i};puts "total: #{t}"' file1 file2 file3
 
Old 09-17-2010, 11:00 AM   #4
dynamics
LQ Newbie
 
Registered: Aug 2009
Location: Guildford
Distribution: Ubuntu Jaunty
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jrosco View Post
There might be better way to do this, but what springs to mind would something like

Code:
file1=$(tail -n 1 /tmp/file1.dat); file1=$(echo ${file1:1:1})

file2=$(tail -n 1 /tmp/file2.dat); file2=$(echo ${file2:1:1})

file3=$(tail -n 1 /tmp/file3.dat); file3=$(echo ${file3:1:1})

result=$(expr $file1 + $file2 + $file3)

echo $result
tail -n 1 file #would get the last line of a file

echo ${var:1:1} #would extract characters at position:length var is the string, the first 1 is the position and the second 1 is the length.

Like I said there could be a better way to do this.

JC
Hi JC
The solution works if my matrix entry are single digits. If they are floating point numbers then the above method does not seems to be working.
Thanks
 
Old 09-17-2010, 11:13 AM   #5
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by dynamics View Post
Hi JC
The solution works if my matrix entry are single digits. If they are floating point numbers then the above method does not seems to be working.
Thanks
Change to:
Code:
result=`echo "($file1 + $file2 + $file3)" | bc`
 
1 members found this post helpful.
Old 09-18-2010, 09:31 PM   #6
jrosco
Member
 
Registered: Aug 2010
Location: Australia
Posts: 37

Rep: Reputation: 4
Didn't realize before, but does the file contain spaces between the characters?

JC

Last edited by jrosco; 09-18-2010 at 09:44 PM.
 
Old 09-19-2010, 08:34 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
awk would work:
Code:
awk 'FNR==3{sum+=$2}END{print sum}' file*.dat
awk is able to do floating point too
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
a new shell for doing mathematical operation tubu_cuteguy Linux - Newbie 5 07-28-2010 07:16 PM
awk convert column matrix to square matrix? johnpaulodonnell Programming 4 04-30-2008 01:45 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration