LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-02-2008, 11:16 AM   #1
nixlearn
LQ Newbie
 
Registered: Nov 2008
Posts: 4

Rep: Reputation: 0
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.
 
Old 12-02-2008, 11:34 AM   #2
jstephens84
Senior Member
 
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098

Rep: Reputation: 102Reputation: 102
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.
 
Old 12-02-2008, 11:45 AM   #3
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,962
Blog Entries: 46

Rep: Reputation: 3188Reputation: 3188Reputation: 3188Reputation: 3188Reputation: 3188Reputation: 3188Reputation: 3188Reputation: 3188Reputation: 3188Reputation: 3188Reputation: 3188
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.
 
Old 12-02-2008, 01:15 PM   #4
nixlearn
LQ Newbie
 
Registered: Nov 2008
Posts: 4

Original Poster
Rep: Reputation: 0
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.
 
Old 12-02-2008, 01:19 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
Old 12-02-2008, 03:19 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
"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/
 
Old 12-02-2008, 03:30 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
 
Old 12-02-2008, 05:36 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by pixellany View Post
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...
 
Old 12-02-2008, 07:27 PM   #9
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Rep: Reputation: 30
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.

Last edited by keysorsoze; 12-02-2008 at 07:32 PM.
 
Old 12-02-2008, 07:31 PM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by keysorsoze View Post
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.
But the OP was not nixlearn? keysorsoze, are you the same person?

Last edited by colucix; 12-02-2008 at 07:34 PM.
 
Old 12-02-2008, 07:32 PM   #11
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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).
 
Old 12-02-2008, 07:37 PM   #12
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Rep: Reputation: 30
"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?
 
Old 12-02-2008, 07:42 PM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by keysorsoze View Post
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\!
 
Old 12-02-2008, 07:43 PM   #14
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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
 
Old 12-02-2008, 07:46 PM   #15
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Rep: Reputation: 30
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
 
  


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
bash: execute a semicolon separated list of commands within string Meson Linux - General 3 10-02-2008 12:25 AM
help with comma separated values and what should be a simple script. zaber Programming 10 03-06-2008 01:58 PM
bash syntax: looping through a comma-separated list David the H. Linux - General 10 09-06-2007 11:23 AM
Sorting a list of words in LISP! Hady Programming 1 05-01-2004 04:29 PM
Reading comma-separated data from file MeLassen Programming 4 04-04-2004 03:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:39 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