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 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.
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.
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.
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.
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:
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 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.