LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 07-23-2013, 11:34 AM   #1
krmarshall87
LQ Newbie
 
Registered: Jul 2012
Posts: 8

Rep: Reputation: Disabled
Compressing a two columned list


I have a list of instances in a server cluster and their matching hostname:

Code:
CLUSTER-A HOST1
CLUSTER-A HOST2
CLUSTER-A HOST3
CLUSTER-A HOST4
CLUSTER-B HOST1
CLUSTER-B HOST2
.
.
I'm sure there is a simpler command (awk or sed) that would get me:

Code:
CLUSTER-A HOST1 HOST2 HOST3 HOST4
CLUSTER-B HOST1 HOST2 HOST3 HOST4
.
.
Currently, I write it to an array and process each string- a real pain, so am curious as to a better method.

From my searches (tough to search for something I can't put a name to), I couldn't find much. But I'm sure this is a common issue.

Last edited by krmarshall87; 07-23-2013 at 11:35 AM. Reason: threw in code tags
 
Old 07-23-2013, 12:33 PM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Try:
Code:
#!/bin/sh

awk '
BEGIN {val="";} 
      {
         if (val == $1) printf(" %s",$2);
         else {
	      if (val != "") printf("\n");
              val=$1;
              printf("%s %s",$1,$2);
         }
       }
END    { printf("\n");}'
Just redirect the input data into the script, and redirect the output to the result.

NOTE: This assumes the input is sorted.

Another note: I see your output has a "CLUSTER-B HOST1 HOST2 HOST3 HOST4", but the input has no such data.

Last edited by jpollard; 07-23-2013 at 12:34 PM.
 
1 members found this post helpful.
Old 07-23-2013, 01:06 PM   #3
krmarshall87
LQ Newbie
 
Registered: Jul 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
I was only putting in an example the . . were to imply further data, so your code works great and does exactly what is needed especially since I do a sort prior. Thanks much for your help!
 
  


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
'E:Malformed line 54 in source list /etc/apt/sources.list (dist parse), E:The list of vsssuccess@gmail.com Linux - Desktop 1 11-17-2010 09:17 AM
Compressing raghavendra2512 Linux - Newbie 2 10-31-2009 03:32 AM
compressing a file micro_xii Linux - Newbie 2 12-23-2006 04:20 AM
compressing forwarded X? theonebeyond Linux - General 0 12-02-2004 04:05 AM
compressing dvds kurrupt Linux - Software 23 09-21-2004 05:15 PM


All times are GMT -5. The time now is 06:44 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration