Hi! I want to convert the lines 1.1,3.19e-4 1.2,3.05e-3 10.5,9.14e8 (as example)
to
1,1 & $3,19 \cdot 10^{-4}$\ etc.. from one file and save these in a new file Rly lost here except I know I should use regexp and MAYBE sed somehow :) Thx for any help _________________________________________________________________ Express yourself instantly with MSN Messenger! Download today it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
On Fri, October 12, 2007 4:54 pm, roland hellström wrote:
Hi! I want to convert the lines 1.1,3.19e-4 1.2,3.05e-3 10.5,9.14e8 (as example)
to
1,1 & $3,19 \cdot 10^{-4}$\ etc.. from one file and save these in a new file Rly lost here except I know I should use regexp and MAYBE sed somehow :) Thx for any help
Assuming that you have those lines in a file called numbers.txt, you can execute the following (all on one line):
cat numbers.txt | tr '.,e' ',^^' | awk -F^ '{printf("%s & $%s \cdot 10^{%s}$\\\n", $1, $2, $3);}'
The output will be:
1,1 & $3,19 \cdot 10^{-4}$\ 1,2 & $3,05 \cdot 10^{-3}$\ 10,5 & $9,14 \cdot 10^{8}$\
Not the most elegant solution, but it works. I hope this is what you were looking for.
Marko A. Jennings wrote:
On Fri, October 12, 2007 4:54 pm, roland hellström wrote:
Hi! I want to convert the lines 1.1,3.19e-4 1.2,3.05e-3 10.5,9.14e8 (as example)
to
1,1 & $3,19 \cdot 10^{-4}$\ etc.. from one file and save these in a new file Rly lost here except I know I should use regexp and MAYBE
sed somehow :)
Thx for any help
Assuming that you have those lines in a file called numbers.txt, you can execute the following (all on one line):
cat numbers.txt | tr '.,e' ',^^' | awk -F^ '{printf("%s & $%s \cdot 10^{%s}$\\\n", $1, $2, $3);}'
The output will be:
1,1 & $3,19 \cdot 10^{-4}$\ 1,2 & $3,05 \cdot 10^{-3}$\ 10,5 & $9,14 \cdot 10^{8}$\
Not the most elegant solution, but it works. I hope this is what you were looking for.
This has the smackings of a CS student trying to get answers to a homework project. The output looks meaningless and the input looks just as meaningless.
If so, then you'll never learn how to do it unless you make your brain think it out itself...
-Ross
______________________________________________________________________ This e-mail, and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail, you are hereby notified that any dissemination, distribution or copying of this e-mail, and any attachments thereto, is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender and permanently delete the original and any copy or printout thereof.
Indeed this is a chalmers student. The purpose of asking though is to learn it, because I find it very hard to learn without seeing an example of it. Thx for the reply it seemed to work well :) Although it would be interesting to see if this could be done with the sed command somehow? Thx in advance
----------------------------------------> Subject: RE: [CentOS] Conversion of text in shell> Date: Fri, 12 Oct 2007 17:29:13 -0400> From: rwalker@medallion.com> To: centos@centos.org>> Marko A. Jennings wrote:>>>> On Fri, October 12, 2007 4:54 pm, roland hellström wrote:>>>>>> Hi! I want to convert the lines>>> 1.1,3.19e-4>>> 1.2,3.05e-3>>> 10.5,9.14e8>>> (as example)>>>>>> to>>>>>> 1,1 & $3,19 \cdot 10^{-4}$\>>> etc.. from one file and save these in a new file>>> Rly lost here except I know I should use regexp and MAYBE>> sed somehow :)>>> Thx for any help>>>> Assuming that you have those lines in a file called>> numbers.txt, you can>> execute the following (all on one line):>>>> cat numbers.txt | tr '.,e' ',^^' | awk -F^ '{printf("%s & $%s \cdot>> 10^{%s}$\\\n", $1, $2, $3);}'>>>> The output will be:>>>> 1,1 & $3,19 \cdot 10^{-4}$\>> 1,2 & $3,05 \cdot 10^{-3}$\>> 10,5 & $9,14 \cdot 10^{8}$\>>>> Not the most elegant solution, but it works. I hope this is>> what you were>> looking for.>> This has the smackings of a CS student trying to get answers to> a homework project. The output looks meaningless and the input> looks just as meaningless.>> If so, then you'll never learn how to do it unless you make> your brain think it out itself...>> -Ross>>>> ______________________________________________________________________> This e-mail, and any attachments thereto, is intended only for use by> the addressee(s) named herein and may contain legally privileged> and/or confidential information. If you are not the intended recipient> of this e-mail, you are hereby notified that any dissemination,> distribution or copying of this e-mail, and any attachments thereto,> is strictly prohibited. If you have received this e-mail in error,> please immediately notify the sender and permanently delete the> original and any copy or printout thereof.>> _______________________________________________> CentOS mailing list> CentOS@centos.org> http://lists.centos.org/mailman/listinfo/centos
_________________________________________________________________ Express yourself instantly with MSN Messenger! Download today it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
roland hellström wrote:
Indeed this is a chalmers student. The purpose of asking though is to learn it, because I find it very hard to learn without seeing an example of it. Thx for the reply it seemed to work well :) Although it would be interesting to see if this could be done with the sed command somehow? Thx in advance
Then let me help you think it out:
Given a text file with this data: 1.1,3.19e-4 1.2,3.05e-3 10.5,9.14e-8 <- I'm guessing this was suppose to be scientific not
Convert to a text file: 1,1 & $3,19 \cdot 10^{-4}$\ 1,2 & $3,05 \cdot 10^{-3}$\ 10,5 & $9,14 \cdot 10^{-8}$\
Look for the pattern match ups: 1.1,3.19e-4 1,1 & $3,19 \cdot 10^{-4}$\
1.2,3.05e-3 1,2 & $3,05 \cdot 10^{-3}$\
10.5,9.14e-8 10,5 & $9,14 \cdot 10^{-8}$\
It looks like: 1) decimals converted to commas 2) commas converted to " & " 3) "e" converted to " \cdot 10^{<>}$\" 4) <> substituted for whatever comes after the "e"
Now if you convert decimals to commas BEFORE you convert commas to " & " then you will have garbage, so you need to convert commas to " & " first, then decimals to commas, then apply the conversion w/ substituion.
Substitution is handled with the () operator like such:
echo "this is a test" | sed -e 's/(.*) is a (.*)/\2 is NOT a \1/'
Will output: "test is NOT a this"
Can be done in 3 steps, sed can take multiple operations if you separate them with ;
<snip mangled thread>
PS Fix your mail client to post in plain text and put your reply text at the bottom of the email instead of the top.
______________________________________________________________________ This e-mail, and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail, you are hereby notified that any dissemination, distribution or copying of this e-mail, and any attachments thereto, is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender and permanently delete the original and any copy or printout thereof.
----------------------------------------> Subject: RE: [CentOS] Conversion of text in shell> Date: Fri, 12 Oct 2007 17:57:54 -0400> From: rwalker@medallion.com> To: centos@centos.org>> roland hellström wrote:>>>> Indeed this is a chalmers student. The purpose of asking>> though is to learn it, because I find it very hard to learn>> without seeing an example of it.>> Thx for the reply it seemed to work well :) Although it would>> be interesting to see if this could be done with the sed>> command somehow?>> Thx in advance>>>> Then let me help you think it out:>> Given a text file with this data:> 1.1,3.19e-4> 1.2,3.05e-3> 10.5,9.14e-8 <- I'm guessing this was suppose to be scientific not>> Convert to a text file:> 1,1 & $3,19 \cdot 10^{-4}$\> 1,2 & $3,05 \cdot 10^{-3}$\> 10,5 & $9,14 \cdot 10^{-8}$\>> Look for the pattern match ups:> 1.1,3.19e-4> 1,1 & $3,19 \cdot 10^{-4}$\>> 1.2,3.05e-3> 1,2 & $3,05 \cdot 10^{-3}$\>> 10.5,9.14e-8> 10,5 & $9,14 \cdot 10^{-8}$\>> It looks like:> 1) decimals converted to commas> 2) commas converted to " & "> 3) "e" converted to " \cdot 10^{<>}$\"> 4) <> substituted for whatever comes after the "e">> Now if you convert decimals to commas BEFORE you convert> commas to " & " then you will have garbage, so you need> to convert commas to " & " first, then decimals to> commas, then apply the conversion w/ substituion.>> Substitution is handled with the () operator like such:>> echo "this is a test" | sed -e 's/(.*) is a (.*)/\2 is NOT a \1/'>> Will output: "test is NOT a this">> Can be done in 3 steps, sed can take multiple operations> if you separate them with ;>> >> PS Fix your mail client to post in plain text and put> your reply text at the bottom of the email instead of> the top.>> ______________________________________________________________________> This e-mail, and any attachments thereto, is intended only for use by> the addressee(s) named herein and may contain legally privileged> and/or confidential information. If you are not the intended recipient> of this e-mail, you are hereby notified that any dissemination,> distribution or copying of this e-mail, and any attachments thereto,> is strictly prohibited. If you have received this e-mail in error,> please immediately notify the sender and permanently delete the> original and any copy or printout thereof.>> _______________________________________________> CentOS mailing list> CentOS@centos.org> http://lists.centos.org/mailman/listinfo/centos
Ok thx for the great reply :)
I used sed 's/([^,]*),([0-9.]*)e(.*)/\1 & $\2 \cdot 10^{\3}$\\/' and got the result resolving everything except the dots converted to commas. I am thinking there is a way to do this without the separator ;? Thx // _________________________________________________________________ Express yourself instantly with MSN Messenger! Download today it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
OK! I finally figured out the solution for all you people out the eager to hear it!!! it was infact very very similar to the last line I sent... this is it sed 's/([^.]*).([^,]*),([^.]*).([^e]*)e(.*)/\1,\2 & $\3,\4 \cdot 10^{\5}$\\/'
omg I feel so h4xx0r figuring that out myself lol Thx for the help all :)
----------------------------------------> Subject: RE: [CentOS] Conversion of text in shell> Date: Fri, 12 Oct 2007 17:57:54 -0400> From: rwalker@medallion.com> To: centos@centos.org>> roland hellström wrote:>>>> Indeed this is a chalmers student. The purpose of asking>> though is to learn it, because I find it very hard to learn>> without seeing an example of it.>> Thx for the reply it seemed to work well :) Although it would>> be interesting to see if this could be done with the sed>> command somehow?>> Thx in advance>>>> Then let me help you think it out:>> Given a text file with this data:> 1.1,3.19e-4> 1.2,3.05e-3> 10.5,9.14e-8 <- I'm guessing this was suppose to be scientific not>> Convert to a text file:> 1,1 & $3,19 \cdot 10^{-4}$\> 1,2 & $3,05 \cdot 10^{-3}$\> 10,5 & $9,14 \cdot 10^{-8}$\>> Look for the pattern match ups:> 1.1,3.19e-4> 1,1 & $3,19 \cdot 10^{-4}$\>> 1.2,3.05e-3> 1,2 & $3,05 \cdot 10^{-3}$\>> 10.5,9.14e-8> 10,5 & $9,14 \cdot 10^{-8}$\>> It looks like:> 1) decimals converted to commas> 2) commas converted to " & "> 3) "e" converted to " \cdot 10^{<>}$\"> 4) <> substituted for whatever comes after the "e">> Now if you convert decimals to commas BEFORE you convert> commas to " & " then you will have garbage, so you need> to convert commas to " & " first, then decimals to> commas, then apply the conversion w/ substituion.>> Substitution is handled with the () operator like such:>> echo "this is a test" | sed -e 's/(.*) is a (.*)/\2 is NOT a \1/'>> Will output: "test is NOT a this">> Can be done in 3 steps, sed can take multiple operations> if you separate them with ;>> >> PS Fix your mail client to post in plain text and put> your reply text at the bottom of the email instead of> the top.>> ______________________________________________________________________> This e-mail, and any attachments thereto, is intended only for use by> the addressee(s) named herein and may contain legally privileged> and/or confidential information. If you are not the intended recipient> of this e-mail, you are hereby notified that any dissemination,> distribution or copying of this e-mail, and any attachments thereto,> is strictly prohibited. If you have received this e-mail in error,> please immediately notify the sender and permanently delete the> original and any copy or printout thereof.>> _______________________________________________> CentOS mailing list> CentOS@centos.org> http://lists.centos.org/mailman/listinfo/centos
_________________________________________________________________ Express yourself instantly with MSN Messenger! Download today it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
roland hellström wrote:
OK! I finally figured out the solution for all you people out the eager to hear it!!! it was infact very very similar to the last line I sent... this is it sed 's/([^.]*).([^,]*),([^.]*).([^e]*)e(.*)/\1,\2 & $\3,\4 \cdot 10^{\5}$\\/'
omg I feel so h4xx0r figuring that out myself lol Thx for the help all :)
I am surprised you got it all in 1 regex, I was aiming more for:
sed 's/,/ & /;s/./,/;s/(.*)e(.*)/\1 \cdot 10^{\2}/'
-Ross
______________________________________________________________________ This e-mail, and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail, you are hereby notified that any dissemination, distribution or copying of this e-mail, and any attachments thereto, is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender and permanently delete the original and any copy or printout thereof.
Ross S. W. Walker wrote:
roland hellström wrote:
OK! I finally figured out the solution for all you people out the eager to hear it!!! it was infact very very similar to the last line I sent...
this is it
sed 's/([^.]*).([^,]*),([^.]*).([^e]*)e(.*)/\1,\2 & $\3,\4 \cdot 10^{\5}$\\/'
omg I feel so h4xx0r figuring that out myself lol Thx for the help all :)
I am surprised you got it all in 1 regex, I was aiming more for:
sed 's/,/ & /;s/./,/;s/(.*)e(.*)/\1 \cdot 10^{\2}/'
whoops, I made a mistake:
sed 's/,/ & /;s/./,/g;s/(.*)e(.*)/\1 \cdot 10^{\2}$\\/'
You need the 'g' option in the second substitute to perform a global, and of course the proper cdot expression.
-Ross
______________________________________________________________________ This e-mail, and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail, you are hereby notified that any dissemination, distribution or copying of this e-mail, and any attachments thereto, is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender and permanently delete the original and any copy or printout thereof.
Ross S. W. Walker wrote:
Ross S. W. Walker wrote:
roland hellström wrote:
OK! I finally figured out the solution for all you people out the eager to hear it!!! it was infact very very similar to the last line I sent...
this is it
sed 's/([^.]*).([^,]*),([^.]*).([^e]*)e(.*)/\1,\2 & $\3,\4 \cdot 10^{\5}$\\/'
omg I feel so h4xx0r figuring that out myself lol Thx for the help all :)
I am surprised you got it all in 1 regex, I was aiming more for:
sed 's/,/ & /;s/./,/;s/(.*)e(.*)/\1 \cdot 10^{\2}/'
whoops, I made a mistake:
sed 's/,/ & /;s/./,/g;s/(.*)e(.*)/\1 \cdot 10^{\2}$\\/'
some wag once said...
"Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems."
:)
Ross S. W. Walker
Ross S. W. Walker wrote:
roland hellström wrote:
OK! I finally figured out the solution for all you people out the eager to hear it!!! it was infact very very similar to the last line I sent...
this is it
sed 's/([^.]*).([^,]*),([^.]*).([^e]*)e(.*)/\1,\2 & $\3,\4 \cdot 10^{\5}$\\/'
omg I feel so h4xx0r figuring that out myself lol Thx for the help all :)
I am surprised you got it all in 1 regex, I was aiming more for:
sed 's/,/ & /;s/./,/;s/(.*)e(.*)/\1 \cdot 10^{\2}/'
whoops, I made a mistake:
sed 's/,/ & /;s/./,/g;s/(.*)e(.*)/\1 \cdot 10^{\2}$\\/'
You need the 'g' option in the second substitute to perform a global, and of course the proper cdot expression.
Ok, not to toot my own horn, but it turns out that doing 3 simple regexs is actually quicker then 1 complex regex, though with today's processors you need to run it over a large set to see:
# wc -l test 20691 test
# time sed 's/([^.]*).([^,]*),([^.]*).([^e]*)e(.*)/\1,\2 & $\3,\4 \cdot 10^{\5}$\\/' test >/dev/null
real 0m0.175s user 0m0.174s sys 0m0.000s
# time sed 's/,/ & $/;s/./,/g;s/(.*)e(.*)/\1 \cdot 10^{\2}$\\/' test >/dev/null
real 0m0.141s user 0m0.139s sys 0m0.001s
So not only is it simpler to divide and conqueror a problem, but often it produces the best results...
-Ross
______________________________________________________________________ This e-mail, and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail, you are hereby notified that any dissemination, distribution or copying of this e-mail, and any attachments thereto, is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender and permanently delete the original and any copy or printout thereof.
Ross S. W. Walker wrote:
Ross S. W. Walker wrote:
roland hellström wrote:
OK! I finally figured out the solution for all you people out the eager to hear it!!! it was infact very very similar to the last line I sent...
this is it
sed 's/([^.]*).([^,]*),([^.]*).([^e]*)e(.*)/\1,\2 & $\3,\4 \cdot 10^{\5}$\\/'
omg I feel so h4xx0r figuring that out myself lol Thx for the help all :)
I am surprised you got it all in 1 regex, I was aiming more for:
sed 's/,/ & /;s/./,/;s/(.*)e(.*)/\1 \cdot 10^{\2}/'
whoops, I made a mistake:
sed 's/,/ & /;s/./,/g;s/(.*)e(.*)/\1 \cdot 10^{\2}$\\/'
You need the 'g' option in the second substitute to perform a global, and of course the proper cdot expression.
you don't need regex:
sed \ -e '/^ *$/d' \ -e 's/,/ & $/' \ -e 's/./,/g' \ -e 's/e/ \cdot 10^{/' -e s'/$/}$\\/' \ /path/to/input/file
now, the exercice is to read the input file directly with LaTeX using TeX macros instead of converting it.
And btw it is not a homework project, it is simply some excercises to help us learn :) ----------------------------------------> Subject: RE: [CentOS] Conversion of text in shell> Date: Fri, 12 Oct 2007 17:29:13 -0400> From: rwalker@medallion.com> To: centos@centos.org>> Marko A. Jennings wrote:>>>> On Fri, October 12, 2007 4:54 pm, roland hellström wrote:>>>>>> Hi! I want to convert the lines>>> 1.1,3.19e-4>>> 1.2,3.05e-3>>> 10.5,9.14e8>>> (as example)>>>>>> to>>>>>> 1,1 & $3,19 \cdot 10^{-4}$\>>> etc.. from one file and save these in a new file>>> Rly lost here except I know I should use regexp and MAYBE>> sed somehow :)>>> Thx for any help>>>> Assuming that you have those lines in a file called>> numbers.txt, you can>> execute the following (all on one line):>>>> cat numbers.txt | tr '.,e' ',^^' | awk -F^ '{printf("%s & $%s \cdot>> 10^{%s}$\\\n", $1, $2, $3);}'>>>> The output will be:>>>> 1,1 & $3,19 \cdot 10^{-4}$\>> 1,2 & $3,05 \cdot 10^{-3}$\>> 10,5 & $9,14 \cdot 10^{8}$\>>>> Not the most elegant solution, but it works. I hope this is>> what you were>> looking for.>> This has the smackings of a CS student trying to get answers to> a homework project. The output looks meaningless and the input> looks just as meaningless.>> If so, then you'll never learn how to do it unless you make> your brain think it out itself...>> -Ross>>>> ______________________________________________________________________> This e-mail, and any attachments thereto, is intended only for use by> the addressee(s) named herein and may contain legally privileged> and/or confidential information. If you are not the intended recipient> of this e-mail, you are hereby notified that any dissemination,> distribution or copying of this e-mail, and any attachments thereto,> is strictly prohibited. If you have received this e-mail in error,> please immediately notify the sender and permanently delete the> original and any copy or printout thereof.>> _______________________________________________> CentOS mailing list> CentOS@centos.org> http://lists.centos.org/mailman/listinfo/centos
_________________________________________________________________ Express yourself instantly with MSN Messenger! Download today it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
roland hellström wrote:
And btw it is not a homework project, it is simply some excercises to help us learn :)
Could you also please learn on how to not abuse a mail program - meaning: Teach your program to *not* break all quotations from previous mails into a single line (2008 characters in this mail)? It makes reading your replies quite a PITA - for all of us.
http://oerks.net/~ralph/Not-So-Hotmail.png
IOW: Please do not use Hotmail.
Ralph
Ross S. W. Walker wrote:
This has the smackings of a CS student trying to get answers to a homework project. The output looks meaningless and the input looks just as meaningless.
It makes total sense - its converting numbers in a datafile to LaTeX format. I've done that quite often as it's easy to make mistakes doing this by hand.
Jeremy
On Fri, 2007-10-12 at 17:15 -0400, Marko A. Jennings wrote:
On Fri, October 12, 2007 4:54 pm, roland hellström wrote:
<snip>
Assuming that you have those lines in a file called numbers.txt, you can execute the following (all on one line):
cat numbers.txt | tr '.,e' ',^^' | awk -F^ '{printf("%s & $%s \cdot 10^{%s}$\\\n", $1, $2, $3);}'
Just a reminder from an old anal-retentive: the creation of the extra process (for cat) and the creation of a pipe is not needed. Use the standard input redirection available in bash.
tr .... < numbers.txt
will do the job.
<snip>
-- Bill