![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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)
|
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
Code:
awk ' { a=$1;sub($1,"",$0) ;arr[a]=arr[a] $0 }
END { for( key in arr ) print key arr[key] } ' file
|
|
#5
|
|||
|
|||
|
They all works! Thank you guys!
|
|
#6
|
|||
|
|||
|
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
|
|||
| Google The UNIX and Linux Forums |