sorting a list into comma separated list
Hello unix gurus, I need some help in processing a list of data into a comma separated paragraph.
Basically I have data in a list form like this: mouse kitten chicken roster screen ball cat dog .. .. .. Into a comma separated list like this: mouse,kitten,chicken,roster,screen,ball,cat,dog Also they need to be printed in a specific with each second line coming before the first so the final result I am looking for is: kitten,mouse,rosteer,chicken,ball,screen,dog,cat Can this be done? Thank you very much for the help. |
This looks like homework? If it is please show good faith and post what code you have done and where it is not doing what you want.
|
Hi,
Yes, it can be done. And as homework it should be done by you. Post what you have tried. Or attempted to date. 'Advanced Bash-Scripting Guide' would be a good online reference. |
Guys, this isn't a a homework assignment just not aware of how to format the output correctly. I was thinking of tr but not sure what tool will take each line and combine into a paragraph. Also perhaps sed could be used to put a space between each line in the list? I will take a look at the scripting document.
Thanks for the help. |
Well, maybe there are a lot of ways to do that. Some sed guru can provide a very short one-liner, but here is a simple solution using pure bash code:
Code:
#!/bin/bash |
"SED guru": Often defined as one who will go to great lengths to do things with sed, even though other tools are better.....;)
I think you can let SED read the lines and then append them to the hold register. Then, when the end of file is encountered, it dumps the hold register and prints. I'll try this when I can. Meanwhile the best SED tutorial is here: http://www.grymoire.com/Unix/ |
I got it quicker than I thought:
But first---assuming this is really not homework---you have to promise to decode this and explain how it works.....:) sed -n -e '{s/$/,/;H}' -e '${x;s/\n/ /gp}' filename > newfilename |
Quote:
|
Another great sed replacement by pixellany. Pixellany do you make uber dollars? I have been patrolling for all sed tips on this forum and your solving problems on all of them. Are you a programmer? Just curious of what all senior members do for a living? Please give advise if possible and how long you have been working with sed to be able to solve almost any problem.
|
Quote:
|
The sed version didn't work on my machine. There are probably better ways to do this in Perl, but here is a quick version:
Code:
#!/usr/bin/perl Code:
telemachus ~ $ perl flipJoin animals |
"But the OP was not nixlearn? keysorsoze, are you the same person?
No I was initially going to repost the question and try my stab at solving two sed statements but did not want to embarass myself. So I edited my post. How do you guys put blue boxes on all your quotes. edit grey boxes. Is it a css class? |
Quote:
[CODE]echo Hello World\![/CODE] will result in Code:
echo Hello World\! |
You can create boxes for quotes and code by wrapping them in bbcode tags. See here for some common ones: http://setiathome.berkeley.edu/bbcode.php
|
Code:
Sweet Thanks I will use this now\! Coluix are you a programmer? I am wondering because I am a sysadmin right now and I honestly don't know half of what you guys know how do you get to the level where you can solve virtually any problem members dish at you? I hope to get senior status one day. Thanks |
All times are GMT -5. The time now is 06:31 PM. |