Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 05-20-2008, 08:39 PM   #1
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
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,605

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

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:

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

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

while(<STDIN>) # Read from stdin

$string=join("\t",@columns); # separate with tab
Give it execute permissions, and run using:
[B]./ < datafile >outputfile


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 > Forums > Non-*NIX Forums > Programming

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

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