Visit The New, Modern Unix Linux Community


Help on file arrangement


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help on file arrangement
# 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 Smilie . any help would be greatly appreciated.
# 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

 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #748
Difficulty: Medium
The ZX81 was a home computer that was produced by Sinclair Research and manufactured in Dundee, Scotland.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Retreive data with arrangement

Hi all I have following part of a big file TTDS00002 Synonyms M1 receptor TTDS00002 Disease Alzheimer's disease TTDS00002 Disease Bronchospasm (histamine induced) TTDS00002 Disease Cognitive deficits TTDS00002 Disease Schizophrenia TTDS00002 Function The muscarinic acetylcholine... (2 Replies)
Discussion started by: kareena
2 Replies

2. UNIX for Dummies Questions & Answers

Tar command to preserve the folder/file arrangement

Hi, I do have question for un tar a file. I have several 'tar'ed files. For example: SRS.tar.bz2. I was trying to untar them in a linux server using the command: tar xvjf SRS.tar.bz2 It worked perfectly. but when I open this file in my mac computer all the files are extracted into a... (7 Replies)
Discussion started by: Lucky Ali
7 Replies

3. UNIX for Advanced & Expert Users

Data re-arrangement

Hi I have a huge problem to solve ASAP. Can someone please help!!! My format is arranged in this format: It has three columns. LOGIN ALIAS REC_ID A BB1 0 A ... (1 Reply)
Discussion started by: Mapilo
1 Replies

4. Shell Programming and Scripting

directories and file arrangement in bash

im trying to write a script that will put files with different extensions into their specified directories In the directory are files of various types, i want to arrange the files on individual directories under their type. There are three distinct types of files: 1) Text documents - files with... (2 Replies)
Discussion started by: elginmulizwa
2 Replies

5. Shell Programming and Scripting

Re-arrangement of data

Dear Frineds, I have a flat file as follows ABCD ABDCWQE POIERAS ADSGASGFG GHJKFHD XBDFGHFGDH POIU IJPFG AFGJFPGOU A;DGUPGU SFSDFDSDFHDSF SDFGHSFDH I want this column to be converted into row like follows ABCD, ABDCWQE, POIERAS, ADSGASGFG, GHJKFHD, XBDFGHFGDH (6 Replies)
Discussion started by: anushree.a
6 Replies

6. Shell Programming and Scripting

sorting/arrangement problem

Hi, I have the following 'sorting' problem. Given the input file: a:b:c:12:x:k s:m:d:8:z:m a:b:c:1:x:k p:q:r:23:y:m a:b:c:3:x:k p:q:r:1:y:m the output I expect is: a:b:c:1:x:k p:q:r:1:y:m s:m:d:8:z:m What happened here is I grouped together lines having the same values for... (7 Replies)
Discussion started by: Abhishek Ghose
7 Replies

7. UNIX for Dummies Questions & Answers

Data arrangement

10 2 1 2 3 4 5 6 7 8 20 3 2 1 3 2 9 8 2 1 Need the data to be arranged: 10 2 1 5 2 6 3 7 4 8 20 3 2 1 1 2 3 8 2 9 please help! (6 Replies)
Discussion started by: bobo
6 Replies

8. UNIX for Dummies Questions & Answers

Cell arrangement

I have a data file with hundreds of lines: I want to place a YES right below the line that say mydata....can someone please help! on the left is my the original data on the right the data format need to be: left > Right mydata > mydata yes > yesno > no mydata > mydata... (12 Replies)
Discussion started by: bobo
12 Replies

9. UNIX for Dummies Questions & Answers

Data arrangement

I have these following data: Home Tom Member List 100 Yes 200 No Home Tom Member List 1 No 2 Yes Home Tome Member List 3 No 400 Yes I want my data to be consistants like this: (4 Replies)
Discussion started by: bobo
4 Replies

10. UNIX for Dummies Questions & Answers

Text file arrangement

Dear UNIX experts: Hi, I have a text file which the contents are arranged vertically down, line by line. How do use a loop (I think) to make it arrange in vertical arrangement with a tab delimitated and write to a new file? Eg: of source file Hello World Good-day Thanks Welcome The... (8 Replies)
Discussion started by: merry susana
8 Replies

Featured Tech Videos