LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-06-2012, 12:17 PM   #1
vitamin307
LQ Newbie
 
Registered: Feb 2012
Posts: 2

Rep: Reputation: Disabled
Post Convert list of values to CSV table


Hi,
I want to convert a list of values like this:
name: John
age: 12
name: Bob
age: 16
name: Steve
name: George
name: Richard
age: 14

To something like:
name: John, age: 12
name: Bob, age: 16
name: Steve,
name: George,
name: Richard, age: 14


I was trying to find a way using SED but no results.
What would be the easy way. It is not necesary to add commas as it can be done late. The main idea is to remove the new line character "\n" from the lines with certain pattern.

Thanks,
 
Old 02-06-2012, 12:37 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
A script in python or perl would be the easiest to write. You could do it all in bash though as well. It depends on what your requirements are. If you post what precise commands you've tried so far, we might be able to point you in the right direction.
 
Old 02-06-2012, 12:58 PM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by vitamin307 View Post
Hi,
I want to convert a list of values like this:
name: John
age: 12
name: Bob
age: 16
name: Steve
name: George
name: Richard
age: 14

To something like:
name: John, age: 12
name: Bob, age: 16
name: Steve,
name: George,
name: Richard, age: 14


I was trying to find a way using SED but no results.
What would be the easy way. It is not necesary to add commas as it can be done late. The main idea is to remove the new line character "\n" from the lines with certain pattern.

Thanks,
Hi,

this worked with the sample data you provided:
Code:
sed '/name:/N;/age:/s/\n/, /' file
 
Old 02-06-2012, 01:37 PM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

The following command should process correctly any number of names without corresponding ages.
Code:
$ cat infile.txt 
name: John
age: 12
name: Bob
age: 16
name: Steve
name: George
name: Mike
name: Sam
age: 25
name: Greg
age: 41
name: Richard
age: 14
$ sed  '/name/{ :a; N; /age:/{s/\(.*\)\n/\1, /; b}; ba}' infile.txt
name: John, age: 12
name: Bob, age: 16
name: Steve
name: George
name: Mike
name: Sam, age: 25
name: Greg, age: 41
name: Richard, age: 14
Substitution command 's///' work on contents of the pattern space. Usually sed reads input line-by-line, so pattern space contains only the current line without trailing newline (which is removed by sed). But the N command allows you to append next line of input to pattern space, with newline inserted between the lines. After that you can do what you want with `\n', remove it for example.

Some comments about the code:
/name/ {...} -- sed executes commands in '{}' only if pattern space matches against the pattern /.../ (contains the word 'name' in this case).
b -- branch unconditionally to the end of the script, that is start next cycle.
:a -- create label named `a'.
ba -- branch unconditionally to label `a'.

To append commas try one of the following filters
Code:
sed 's/[^0-9]$/&,/'
sed '/age:/!s/$/,/'
EDIT: @crts -- I apologize for massive editing of this post. It looks like some concurrency took place.

Last edited by firstfire; 02-06-2012 at 02:49 PM. Reason: Simplify.
 
Old 02-06-2012, 02:25 PM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
After reading firstfire's solution I realized that you want commas after the name even if there is no 'age' following. I did not see that at first. So here is an altered version of my previous solution:
Code:
sed '/name:/N;/age:/s/\n/, /;t;s/\n\|$/,&/g' file
 
Old 02-07-2012, 02:19 AM   #6
vitamin307
LQ Newbie
 
Registered: Feb 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks a lot!
It works!!!
 
Old 02-07-2012, 05:07 PM   #7
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Hi crts, I understand your initial solution in #4 as sed is greedy and replaces (i.e. removes) only the last occurrence of a newline here. But how is #5 supposed to work? It’s only working for an odd number of name entries without following age for me.
Code:
$ cat file
name: Mike
name: Sam
age: 25
$ sed '/name:/N;/age:/s/\n/, /;t;s/\n\|$/,&/g' file
name: Mike,
name: Sam,
age: 25,
 
Old 02-10-2012, 02:53 PM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by Reuti View Post
Hi crts, I understand your initial solution in #4 as sed is greedy and replaces (i.e. removes) only the last occurrence of a newline here. But how is #5 supposed to work? It’s only working for an odd number of name entries without following age for me.
Code:
$ cat file
name: Mike
name: Sam
age: 25
$ sed '/name:/N;/age:/s/\n/, /;t;s/\n\|$/,&/g' file
name: Mike,
name: Sam,
age: 25,
Hi Reuti,

well observed. To be honest I only tested it with the sample data provided and did not alter it to verify it for all possible combinations.
 
  


Reply


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
How to read CSV data and compare the column values and then write them in new file VijayaRaghavanLakshman Linux - Newbie 9 01-26-2012 09:02 PM
[SOLVED] splitting of comma separated values file (horizontal) into a list (vertical ) list Donoughue Linux - Newbie 3 10-20-2011 01:18 PM
Reading a CSV text file and storing the values in Oracle Database table shafi2all Linux - Newbie 3 04-17-2008 12:19 PM
looking for a perl script to convert html table data into a csv file swiftguy121 Linux - Software 2 04-25-2007 07:28 PM
how can I iterate over csv values using Ruby Joseph Schiller Programming 1 02-20-2006 08:29 PM

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

All times are GMT -5. The time now is 11:08 PM.

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