The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
To cut entire column from a file and apend it to another file as another column sakthifire Shell Programming and Scripting 4 06-25-2008 05:27 AM
Flat file Krishnaramjis Shell Programming and Scripting 9 05-08-2008 11:28 PM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 09:57 AM
XML to flat file balireddy_77 Shell Programming and Scripting 2 04-13-2007 06:57 AM
Column names in flat files srivsn Shell Programming and Scripting 1 12-27-2005 06:47 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-15-2006
jambesh's Avatar
jambesh jambesh is offline
Registered User
  
 

Join Date: Aug 2006
Location: Pune,India
Posts: 137
Look up column in a flat file

Here is on more go ! Need a shortcut for my problem !

problem is i have a look_update with fixed sequence of column
that is :

MANDT:SERAIL:SERSCHA:SEREX:EQTYP:BSTVP
I will be getting data in a flat file having same number of column but the sequence could be different in each time

for example
SERAIL MANDT SERSCHA SEREX EQTYP BSTVP
510 1 f g s r
510 2 g S t
510 3 g S 2
510 4 g S 2
510 Z001 g S
510 Z004 f gr S 2
510 ZPM1 f er M 1
510 ZPM2 76 M 1

here the sequence of the column is not what it in look up string
it should be in the sequence of look up string .
I want to arrange the data of this table in look up sequence and put it into another filnal_data file

each time the clientdata may come in differnt sequence but i have arrange the column in the sequence of lookup_string and append it to final table

Hope u all got my problem ! please assist
  #2 (permalink)  
Old 09-15-2006
Dhruva's Avatar
Dhruva Dhruva is offline
Registered User
  
 

Join Date: Mar 2006
Location: India
Posts: 255
jambesh can you explain again and clearly that will help us to understand and you will get more accurate answer
  #3 (permalink)  
Old 09-15-2006
jambesh's Avatar
jambesh jambesh is offline
Registered User
  
 

Join Date: Aug 2006
Location: Pune,India
Posts: 137
Dhruv,
Here is the problem again ..
I have a fixed look up file whose column are in fixed sequence say..
"MANDT SERAIL SERSCHA SEREX EQTYP BSTVP"
i have stored this sequence in a variable ..
Now

I am getting data file whose column sequence could be differnt from that i have mention in the look up string .

I want to resuffle this data file according to the column sequence of the look_up string.

data file column sequence could be

case -1 sequence
----------

SERSCHA SEREX EQTYP BSTVP MANDT SERAIL
333 4343 fdfd fdfdf dssds fdfdf
343 343 rere 43 fdf 4343

case -2 sequence :
-------------------
SEREX EQTYP BSTVP MANDT SERAIL SERSCHA
121 3232 323 ddd sd 223


see the sequence of these file each time sequence of column varies .
the final reaange of these file should be based on the column of the look up[ string always.



Ok
1

Now i want to resuffle this data file so that the column starting with

MANDT should come first then SERAIL ...then SERSCHA SEREX EQTYP

and the final resuffle data should append to a file name say final_data.txt
  #4 (permalink)  
Old 09-15-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398

Code:
awk -v str="MANDT SERAIL SERSCHA SEREX EQTYP BSTVP" '
NR == 1 {
    n=split( str , arr , " " )
    for( i = 1; i <= n ; ++i )
        arr_ac[$i]=i
    for( i = 1; i <= n ; ++i )
        col[i]=arr_ac[arr[i]]
    print str 
}
NR > 1 {
    for(i = 1; i <= n ; ++i )
        printf("%s ",$col[i])
    printf("\n")
}
' file >> final_data.txt

  #5 (permalink)  
Old 09-18-2006
Dhruva's Avatar
Dhruva Dhruva is offline
Registered User
  
 

Join Date: Mar 2006
Location: India
Posts: 255
check this post
rearrange file based on lookup file
  #6 (permalink)  
Old 09-18-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,555
here's an alternative in Python:

Code:
def transpose(matrix):        
        return [[matrix[y][x] for y in range(len(matrix))]for x in range(len(matrix[0]))]


all = open("input.txt").readlines()
lookupstring = ['MANDT', 'SERSCHA','SERAIL' , 'SEREX', 'EQTYP', 'BSTVP']
listing = [ i.split() for i in all ] 
results =  transpose(listing)
final= [ r for items in lookupstring for r in results if items == r[0] ]
for i in transpose(final):
        print ','.join(i)

Input:

Code:
MANDT SERAIL EQTYP SERSCHA SEREX BSTVP
510   hsgdfs 44    sercha  sex1  bst233
510   bg     89    fg      23    98
510   gh     89    we      sew   mn

Output:

Code:
/home>python test.py
MANDT,SERSCHA,SERAIL,SEREX,EQTYP,BSTVP
510,sercha,hsgdfs,sex1,44,bst233
510,fg,bg,23,89,98
510,we,gh,sew,89,mn

Closed Thread

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:33 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0