LinuxQuestions.org
Review your favorite Linux distribution.
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 05-20-2008, 08:39 PM   #1
skuz_ball
LQ Newbie
 
Registered: Nov 2007
Location: Melbourne, Australia
Posts: 14

Rep: Reputation: 0
adding constant (number) to a number in all rows


Hi all,

I have a file which has temperatures in Kelvin in a column and would like to convert these numbers to Celsius. I think sed would be appropriate to use in this instance but having multiple lines in the file has confused me a bit. im trying to do this in a shell script. I usually use ksh to do this work but am open to any suggestions.

eg file1:

1 2 3 4
1 2 3 5
1 2 3 3
1 2 3 7

Would like to take away 3 from last column so file2 looks like this:

1 2 3 1
1 2 3 2
1 2 3 0
1 2 3 4

Any help would be greatly apprciated.
 
Old 05-20-2008, 09:46 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
awk is your friend.

Code:
cat file1 | awk '{print $1 " " $2 " " $3 " "  $4 + 273}' > file2

That is how to convert Celsius to Kelvin. I don't understand your last conversion to take 3 away from the last column.

In sed it would be:

Code:
sed s/3\ *$/0/
jlinkels
 
Old 05-20-2008, 09:57 PM   #3
rlhartmann
Member
 
Registered: Mar 2008
Posts: 73

Rep: Reputation: 16
Here's a perl script to do it:

Quote:
#!/usr/bin/perl
while(<STDIN>) # Read from stdin
{

chomp;
@columns=split(/\s/,$_);
$columns[3]-=3;
$string=join("\t",@columns); # separate with tab
print("$string\n");
}
Give it execute permissions, and run using:
[B]./kelvin.pl < datafile >outputfile
 
  


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
data with same number of rows and coloumns C/C++ fs11 Programming 2 04-29-2008 10:17 PM
will adding more ram reduce the number of page faults? jwn7 General 2 01-16-2005 04:31 PM
what is a major number and a minor number ? Menestrel Linux - Newbie 2 10-11-2004 07:53 AM
how do you edit your virtual console number? (or VT number 3) jjorloff1 Linux - General 2 04-03-2004 07:21 PM
why there is a need for minor number and major number for monitor/keyboard in pc? tripathi Solaris / OpenSolaris 1 11-07-2003 09:36 AM

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

All times are GMT -5. The time now is 09:54 PM.

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