The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sorting/arrangement problem Abhishek Ghose Shell Programming and Scripting 7 05-25-2007 06:19 AM
Data arrangement bobo UNIX for Dummies Questions & Answers 6 09-21-2006 07:36 AM
Cell arrangement bobo UNIX for Dummies Questions & Answers 12 08-21-2006 05:42 PM
Data arrangement bobo UNIX for Dummies Questions & Answers 4 08-18-2006 07:02 AM
Text file arrangement merry susana UNIX for Dummies Questions & Answers 8 06-17-2005 01:11 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 03-09-2007
Registered User
 

Join Date: Mar 2007
Location: Manila
Posts: 25
Help on file arrangement

Can anyone help me on this. I have a file that looks like this:

color red green blue
color pink yellow
number one two
gender male
gender female


The output would look like this:
color red green blue pink yellow
number one two
gender male female


I have over 5000 rows and i dont want to do it manually . any help would be greatly appreciated.
Reply With Quote
Forum Sponsor
  #2  
Old 03-09-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,580
if you have Python and you know the language, here's an alternative:
Code:
#!/usr/bin/python
import os,glob
def arrange(filename):
   seen = {}
   for line in open(filename):
      line = line.strip().split(' ',1)
      key,val = line[0], line[1]
      if not seen.has_key( key ):
         seen[key] = [val]
      else:
         seen[key].append(val)
   return seen

os.chdir("/test")
for fi in glob.glob("*.txt"):
   if os.path.isfile(fi):
      d = arrange(fi)
      o = open("temp","a")
      for i,j in d.iteritems():
         o.write("%s %s\n" % (i,' '.join(j)) )
      o.close()
      os.rename("temp",fi)
Reply With Quote
  #3  
Old 03-09-2007
dj -------
 

Join Date: Feb 2007
Location: Cochin/Bangalore, India
Posts: 418
Please give a try on this...

key_field=$(awk -F" " '{ print $1;}' filename | uniq)
for each_key in $key_field;
do
awk -F" " -v ind=$each_key '{ if($1==ind) { for(i=2;i<=NF;i++) { s = s" "$i;}}} END { print ind s} ' filename;
done
Reply With Quote
  #4  
Old 03-09-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
awk ' { a=$1;sub($1,"",$0) ;arr[a]=arr[a] $0 } 
END { for( key in arr ) print key arr[key] } ' file
Reply With Quote
  #5  
Old 03-10-2007
Registered User
 

Join Date: Mar 2007
Location: Manila
Posts: 25
They all works! Thank you guys!
Reply With Quote
  #6  
Old 03-10-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,609
Code:
awk '{ if( prev == $1 ) { for( i=2; i<=NF; i++ ) { printf "%s ", $i }; } else { printf "\n"; prev = $1; for( i=1; i<=NF; i++ ) { printf "%s ", $i } } }END { print "\n"} ' filename
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:13 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0