LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sorting a list into comma separated list (https://www.linuxquestions.org/questions/linux-newbie-8/sorting-a-list-into-comma-separated-list-687744/)

nixlearn 12-02-2008 10:16 AM

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.

jstephens84 12-02-2008 10:34 AM

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.

onebuck 12-02-2008 10:45 AM

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.

nixlearn 12-02-2008 12:15 PM

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.

colucix 12-02-2008 12:19 PM

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
count=0
while read line
do
  if [ $((count % 2)) -ne 0 ]
  then
    if [ -z $result ]
    then
      result=$line,$pre
    else
      result=$result,$line,$pre
    fi
  else
    pre=$line
  fi
  ((count++))
done < testfile  # the name of the input file here
echo $result


pixellany 12-02-2008 02:19 PM

"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/

pixellany 12-02-2008 02:30 PM

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

colucix 12-02-2008 04:36 PM

Quote:

Originally Posted by pixellany (Post 3362128)
But first---assuming this is really not homework---you have to promise to decode this and explain how it works.....:)

Or why it does not work! He he he... ;)

keysorsoze 12-02-2008 06:27 PM

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.

colucix 12-02-2008 06:31 PM

Quote:

Originally Posted by keysorsoze (Post 3362362)
Thank you everyone for the help, I promise this was not a homework assignment I just did not want to use real data since it was sensitive.

Please no one reveal what this sed statement does I will try my best to decode it. I did not even know this was possible with sed. Pixellany do you make uber dollars? It seems like there is nothing you can't solve with sed. I will also work on explaining the shell script since it confused me as well.

Thanks for all the help, this forum is great.

:confused: But the OP was not nixlearn? keysorsoze, are you the same person? :confused:

Telemachos 12-02-2008 06:32 PM

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
use strict;
use warnings;

my @lines;

while (<>) {
  chomp;
  push @lines, $_;
}

my $count = 0;

while ( $lines[$count] and $lines[$count + 1] ) {
  ( $lines[$count], $lines[$count + 1] ) =
  ( $lines[$count + 1], $lines[$count] );
  $count += 2;
}

print join ",", @lines;
print "\n";

Save it as (for example) flipJoin and run it with perl flipJoin animalsFile. Output:
Code:

telemachus ~ $ perl flipJoin animals
kitten,mouse,roster,chicken,ball,screen,dog,cat

If the number of entries isn't even, then the final entry just gets listed last (ie, it can't be flipped).

keysorsoze 12-02-2008 06:37 PM

"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?

colucix 12-02-2008 06:42 PM

Quote:

Originally Posted by keysorsoze (Post 3362368)
How do you guys put blue boxes on all your quotes.

edit grey boxes. Is it a css class?

Well, technically I don't know. We simply use quote and code tags. For example the following line

[CODE]echo Hello World\![/CODE]

will result in
Code:

echo Hello World\!

Telemachos 12-02-2008 06:43 PM

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

keysorsoze 12-02-2008 06:46 PM

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

Telemachos 12-02-2008 06:47 PM

They are css controlled. The nice thing about this is that if you create a userContent.css file for Firefox, then you can override the defaults. All code or pre tags I see, on any website, have the size, color and font I prefer. It makes things much easier on my eyes. See here for more info: http://www.mozilla.org/unix/customizing.html

Telemachos 12-02-2008 06:49 PM

Double post - sorry.

jstephens84 12-02-2008 08:08 PM

Quote:

Originally Posted by keysorsoze (Post 3362381)
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

The best way is by doing what you are doing now. Helping other members. I have learned more by helping others and trying to solve their problems while creating my own problems to solve. Also another way is just scan the forums and see what questions where asked and how they were solved. You will never know it all. I have really come to accept that learning + computers = life long journey with plenty of side stops along the way.

pixellany 12-02-2008 08:20 PM

Quote:

Originally Posted by keysorsoze (Post 3362362)
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.

I make only modest dollars, and not for anything to do with Linux and programming. In fact, I would be a terrible programmer. I am an aerospace systems engineer, with emphasis on camera systems.

I somehow got attached to SED---maybe because I like puzzles. I have only been using SED for 2years, but have been marginally competent for less than a year.

Telemachos 12-02-2008 08:32 PM

Not to rain on the parade unduly, but the sed command doesn't seem to me to flip the lines (it also leaves a trailing final comma). Output:
Code:

telemachus ~ $ sed -n -e '{s/$/,/;H}' -e '${x;s/\n/ /gp}' < animals
 mouse, kitten, chicken, roster, screen, ball, cat, dog,

(I'm using GNU sed version 4.1.5 if that helps.)

pixellany 12-02-2008 11:29 PM

Quote:

Originally Posted by Telemachos (Post 3362458)
Not to rain on the parade unduly, but the sed command doesn't seem to me to flip the lines (it also leaves a trailing final comma). Output:
Code:

telemachus ~ $ sed -n -e '{s/$/,/;H}' -e '${x;s/\n/ /gp}' < animals
 mouse, kitten, chicken, roster, screen, ball, cat, dog,

(I'm using GNU sed version 4.1.5 if that helps.)

Well, darn!!
I missed the part about flipping the words. (Getting rid of the last comma is easy.)

Once we have the comma-separated list, I assume that AWK could do the rearranging. I'll bet that SED can do it also, but it's not jumping out at me.

But then, OP has gone missing, so do I really have to work on this??....;)

pixellany 12-02-2008 11:56 PM

Here's a start...
Code:

sed -r 's/([^ ]+) ([^ ]+)/\2 \1/g' filename
This does pairwise reversal of word order, eg

one two three four
becomes:
two one four three

1 2 3 4 5
becomes
2 1 4 3 5

Telemachos 12-03-2008 06:21 AM

Quote:

Originally Posted by pixellany (Post 3362583)
But then, OP has gone missing, so do I really have to work on this??....;)

Not as far as I'm concerned, but then again I'm happy because I've already solved it for Perl. So, I guess it all depends on how much you want to solve it for sed. It looks like you've already done it...


All times are GMT -5. The time now is 06:18 PM.