LinuxQuestions.org
Help answer threads with 0 replies.
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 04-02-2013, 07:38 AM   #1
Jykke
Member
 
Registered: Sep 2005
Posts: 201

Rep: Reputation: 19
How do I break a tabular list into pairs?


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?

Last edited by Jykke; 04-02-2013 at 08:25 AM.
 
Old 04-02-2013, 07:59 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
sed:
Code:
sed -r 's/([^,]*,[^,]*),\s*/\1\n/g' inFile
awk:
Code:
awk 'BEGIN{RS="[,\n ]+"}ORS=NR%2?",":"\n"' inFile
 
2 members found this post helpful.
Old 04-02-2013, 08:24 AM   #3
Jykke
Member
 
Registered: Sep 2005
Posts: 201

Original Poster
Rep: Reputation: 19
Thanks, seems to give extra linefeed in the end of the line but that I can pipe through a second sed
 
Old 04-02-2013, 08:30 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by millgates View Post
sed:
Code:
sed -r 's/([^,]*,[^,]*),\s*/\1\n/g' inFile
awk:
Code:
awk 'BEGIN{RS="[,\n ]+"}ORS=NR%2?",":"\n"' inFile
Something puzzling... the awk works perfectly but the sed OutFile contains unexpected blank lines.

Daniel B. Martin
 
Old 04-02-2013, 08:38 AM   #5
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by danielbmartin View Post
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.

Last edited by millgates; 04-02-2013 at 08:40 AM.
 
Old 04-02-2013, 10:20 AM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by millgates View Post
Code:
awk 'BEGIN{RS="[,\n ]+"}ORS=NR%2?",":"\n"' inFile
OP asked for a blank following each comma. Your excellent awk is easily changed to provide this.
Code:
awk 'BEGIN{RS="[,\n ]+"}ORS=NR%2?", ":"\n"' $InFile
Daniel B. Martin
 
Old 04-02-2013, 10:27 AM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by millgates View Post
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.
Please elaborate. The (\s*\S) part is way over my head.

Daniel B. Martin
 
Old 04-02-2013, 10:35 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
"\s" is simply a shortcut for "[[:space:]]", supported in some flavors of regex. "\S" is the inverse.

The regular expression section of the grep info page has a good breakdown of all the supported expressions in the regex supported by these programs.
 
1 members found this post helpful.
  


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
Netbeans 7.3: Program does not break at break points when debugging JavaScript OtagoHarbour Programming 0 02-22-2013 02:44 AM
All pairs list from a file rattlesnakejoe Programming 4 07-14-2009 01:50 AM
tabular to xml and back to tabular ebsbel Programming 3 03-14-2007 06:43 AM
SQL Reporting Services and Non-Tabular Data carl.waldbieser Programming 1 10-11-2005 09:10 AM
Latex, width of a tabular table? fei Programming 5 05-23-2005 09:11 AM

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

All times are GMT -5. The time now is 03:23 AM.

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