Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Can't do temperatures? Preposterous!' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Can't do temperatures? Preposterous!
Authored by: babbage on Jun 01, '01 11:36:44AM
Why, just take a look at Mastering Regular Expressions, page 199. It's a Perl one-liner!
11:17:54am :chris% echo 73F>temp.txt
11:18:21am :chris% cat temp.txt
73F
11:18:25am :chris% perl -pi -e 's[(d+(.d*)?)Fb]{sprintf "%.0fC", ($1-32) * 5/9}eg' temp.txt
11:20:18am :chris% cat temp.txt
23C
11:20:21am :chris%

Some slight editing of the printf part should give you conversions the other way too:

11:27:58am :chris% echo 73F > temp.txt
11:28:01am :chris% cat temp.txt
73F
11:28:09am :chris% perl -pi -e 's[(d+(.d*)?)Fb]{sprintf "%.0fC", ($1-32) * 5/9}eg' temp.txt
11:28:17am :chris% cat temp.txt
23C
11:28:19am :chris% perl -pi -e 's[(d+(.d*)?)Cb]{sprintf "%.0fF", ($1 * 9 / 5) + 32}eg' temp.txt
11:28:28am :chris% cat temp.txt
73F
11:28:30am :chris%

Of course this is mainly only useful if you're insane, but then hey if you are then welcome to the world of Perl scripting... :)

[ Reply to This | # ]