UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 27th, 2012, 08:24 PM
rei125 rei125 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 5 rei125 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 59 sec
Reputation Power: 0
Help on unix script to join similar lines of input

Hi,

I have been thinking of how to script this but i have no clue at all..
Could someone please help me out or give me some idea on this?

I would like to group those lines with the same first variable in each line, joining the 2nd variables with commas.
Let's say i have the following input.
Code:
aa c1
aa c2
aa c3
cc d1
dd e1
dd e2
ee f1


I would like the output to be like this.
Code:
aa c1,c2,c3
cc d1
dd e1,e2
ee f1


Could this be easily done with bash script?
Or should i try perl script then?
I'm a beginner in bash script and perl.

Thank you.

Reply With Quote
  #2  
Old June 27th, 2012, 11:01 PM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 16 m 23 sec
Reputation Power: 1485
Are the lines sorted? If so it should be reasonably simple to do in almost any scripting language from perl to awk or 'plain' bash script.

'All' you need do is track the value in the first column as you read the input and if it is the same as the last value read append the value in the second column to a variable. If the value in the first column is different (and you have output to show) do the output and clear the variable down and reset the current value of the first column.

Code:
awk 'BEGIN { x=0; c1=""; c2="" }
   {
     if ($1 != c) { if (c1 != "") { print c1, c2 } c1=$1;c2=$2;x=1 }
     else { x += 1 ; if (x == 0) { c2=$2 } else { c2=c2","$2 } }
   }
   END { if (x !=0) { print c1, c2 } }' your_input_file.txt
__________________
The moon on the one hand, the dawn on the other:
The moon is my sister, the dawn is my brother.
The moon on my left and the dawn on my right.
My brother, good morning: my sister, good night.
-- Hilaire Belloc

Reply With Quote
  #3  
Old June 29th, 2012, 02:49 AM
rei125 rei125 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 5 rei125 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 59 sec
Reputation Power: 0
Quote:
Originally Posted by SimonJM
Are the lines sorted? If so it should be reasonably simple to do in almost any scripting language from perl to awk or 'plain' bash script.

'All' you need do is track the value in the first column as you read the input and if it is the same as the last value read append the value in the second column to a variable. If the value in the first column is different (and you have output to show) do the output and clear the variable down and reset the current value of the first column.

Code:
awk 'BEGIN { x=0; c1=""; c2="" }
   {
     if ($1 != c) { if (c1 != "") { print c1, c2 } c1=$1;c2=$2;x=1 }
     else { x += 1 ; if (x == 0) { c2=$2 } else { c2=c2","$2 } }
   }
   END { if (x !=0) { print c1, c2 } }' your_input_file.txt


sorry, it doesnt work for me...

Anyway i've found a short solution to this.
${input} is the filename for the input file.

Code:
for m in `cat ${input} | awk '{print $1}' | sort | uniq `
do
        var=`grep "^${m} " ${output} | awk '{print $2}' | tr '\n' ',' | sed '$s/,$//'`
        echo "${m} ${var}"
done

Thanks anyway.

Reply With Quote
  #4  
Old June 29th, 2012, 08:11 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 16 m 23 sec
Reputation Power: 1485
Gah! Simple typo, sorry!

Code:
awk 'BEGIN { x=0;c1="";c2="" }
{
 if ($1 != c1) { if (c1 != "" ) { print c1,c2 } c1=$1;c2=$2;x=1 }
 else { x += 1; if (x == 0) { c2=$2 } else { c2=c2","$2 } }
}
END { if (x != 0) { print c1,c2 } }' Your_input_file.txt


Glad you got it sorted another way. You could replace the | sort | uniq with a simple sort -u if you wished.

Reply With Quote
  #5  
Old July 1st, 2012, 09:09 PM
rei125 rei125 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 5 rei125 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 59 sec
Reputation Power: 0
Quote:
Originally Posted by SimonJM
Gah! Simple typo, sorry!

Code:
awk 'BEGIN { x=0;c1="";c2="" }
{
 if ($1 != c1) { if (c1 != "" ) { print c1,c2 } c1=$1;c2=$2;x=1 }
 else { x += 1; if (x == 0) { c2=$2 } else { c2=c2","$2 } }
}
END { if (x != 0) { print c1,c2 } }' Your_input_file.txt


Glad you got it sorted another way. You could replace the | sort | uniq with a simple sort -u if you wished.


It works now!
I need to study yoru command...
thank you so much!

Reply With Quote
  #6  
Old July 9th, 2012, 11:05 AM
LKBrwn_DBA's Avatar
LKBrwn_DBA LKBrwn_DBA is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2006
Posts: 748 LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 1 h 41 m 3 sec
Reputation Power: 348
Quote:
Originally Posted by rei125
It works now!
I need to study yoru command...
thank you so much!

Better use this:
Code:
awk '{if(k!=$1)c=""; a[$1]=a[$1] c $2; c=",";k=$1}
END {for (i in a) print i,a[i]}
' Your_input_file.txt | sort

__________________

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Help on unix script to join similar lines of input

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap