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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
|
12-02-2008, 11:16 AM
|
#1
|
LQ Newbie
Registered: Nov 2008
Posts: 4
Rep:
|
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.
|
|
|
12-02-2008, 11:34 AM
|
#2
|
Senior Member
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098
Rep:
|
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.
|
|
|
12-02-2008, 11:45 AM
|
#3
|
Moderator
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,962
|
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.
|
|
|
12-02-2008, 01:15 PM
|
#4
|
LQ Newbie
Registered: Nov 2008
Posts: 4
Original Poster
Rep:
|
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.
|
|
|
12-02-2008, 01:19 PM
|
#5
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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
|
|
|
12-02-2008, 03:19 PM
|
#6
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
"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/
|
|
|
12-02-2008, 03:30 PM
|
#7
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
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
|
|
|
12-02-2008, 05:36 PM
|
#8
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by pixellany
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...
|
|
|
12-02-2008, 07:27 PM
|
#9
|
Member
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295
Rep:
|
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.
|
|
|
12-02-2008, 07:31 PM
|
#10
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by keysorsoze
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.
|
|
|
12-02-2008, 07:32 PM
|
#11
|
Member
Registered: May 2007
Distribution: Debian
Posts: 754
Rep:
|
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).
|
|
|
12-02-2008, 07:37 PM
|
#12
|
Member
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295
Rep:
|
"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?
|
|
|
12-02-2008, 07:42 PM
|
#13
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by keysorsoze
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
|
|
|
12-02-2008, 07:43 PM
|
#14
|
Member
Registered: May 2007
Distribution: Debian
Posts: 754
Rep:
|
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
|
|
|
12-02-2008, 07:46 PM
|
#15
|
Member
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295
Rep:
|
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 11:39 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|