[SOLVED] How do I break a tabular list into pairs?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am looking for a sed or awk one liner for following.
I have a table (usually) 8 numbers/line, separated with comma.
The format in this special case is, for example
0, 10., 0.0001, 50., 0.0002, 84.0, 0.0003, 11.6,
0.0004, 15., 0.0005, 18.5, 0.0006, 28.7, 0.0007, 25.4,
0.0008, 28., 0.0009, 39., 0.001, 353.4, 0.0011, 60.8,
0.0012, 89., 0.0013, 114., 0.0014, 146.1, 0.0015, 17.9,
I would like to pass this through sed and get:
0, 10.
0.0001, 50.
0.0002, 84.0
0.0003, 11.6
0.0004, 15.
0.0005, 18.5
0.0006, 28.7
0.0007, 25.4
0.0008, 28.
0.0009, 39.
0.001, 353.4
0.0011, 60.8
0.0012, 89.
0.0013, 114.
0.0014, 146.1
0.0015, 17.9
I try sed 's/,/\n/2' but it only replaces the second occurrence on each line, not every second occurrence. Any ideas how to perfect it?
Something puzzling... the awk works perfectly but the sed OutFile contains unexpected blank lines.
Daniel B. Martin
Yes, the sed takes characters up to the second comma encountered, and puts a newline after that. But if the inFile already contains a newline there, there will be two newlines then.
This one is a bit longer, but should take care of the spaces:
Code:
sed -r 's/([^,]*,[^,]*),(\s*\S)/\1\2\n/g' inFile
What it does is that it checks that there is at least one non-whitespace character after the pattern (= the pattern is not at the end of the line) before the substitution.
Maybe someone can find a simpler solution...
Of course, the sed examples assume there will be an even number of numbers in each line.
What it does is that it checks that there is at least one non-whitespace character after the pattern (= the pattern is not at the end of the line) before the substitution.
Please elaborate. The (\s*\S) part is way over my head.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.