Look up column in a flat file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Look up column in a flat file
# 1  
Old 09-15-2006
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  
Old 09-15-2006
jambesh can you explain again and clearly that will help us to understand and you will get more accurate answer
# 3  
Old 09-15-2006
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  
Old 09-15-2006
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  
Old 09-18-2006
# 6  
Old 09-18-2006
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to change the format of the date column in a flat file?

Hi, i have a flat file namely temp.txt with this data below ID|name|contact_date 101|Kay|2013-12-26 102|let|2013-12-26 I need to modify the date data in the flat file into MM/DD/YYYY HH24:MI:SS format let me know the code for this. Thank you! (5 Replies)
Discussion started by: srikanth_sagi
5 Replies

2. Shell Programming and Scripting

Find the position of a field/column in a flat file

Hi, Let say I have a file which has around 400 fields. SampleFile ========= PATIENTID|FACILITY|................|TIME_LAST_VISITED_BY_MD|.....|STATUS| How is it possible to find out which field is TIME_LAST_VISITED_BY_MD?fro example by seeing the above structure we can saw FACILITY... (5 Replies)
Discussion started by: machomaddy
5 Replies

3. Shell Programming and Scripting

Sort flat file by 3rd column in perl

Hello Guys I want to sort a flat file by the third column (numeric ) and store it in some other name I/P 9924873|20111114|00000000000013013|130|13|10/15/2010 12:36:22|W860944|N|00 9924873|20111114|00000000000013009|130|09|10/15/2010 12:36:22|W860944|N|00... (12 Replies)
Discussion started by: Pratik4891
12 Replies

4. Shell Programming and Scripting

calculating column summation in a directory of flat files

Hello Guru s I need your kind help to solve my below issue I have a directory of flat files and have to calculate sum of some columns from the flat file . Say for flat file 302 I need the column summation of 2 and 3 rd column For flat file 303 I need the column summation of 5 and... (2 Replies)
Discussion started by: Pratik4891
2 Replies

5. Shell Programming and Scripting

Summation of column value in flat file

Hello Guys Please find my below requirement I have a flat file with column headers in first line and data The structure like below col1 col2 col3 A 1 2 B 3 4 C 5 6 Say I have to take the summation of col2 (that will depend on the... (2 Replies)
Discussion started by: Pratik4891
2 Replies

6. Shell Programming and Scripting

How to read the first column in a flat file with ~ as delimiter

I have one flat file like below id1~col~batch1 id2~col2~batch2 id3~col3~batch3 I need to read the first column one by one and I need to write one db2 query based on that column1 Like for (i=0;i<=10;i++) do insert into table column (con_id) values (select column from table where... (4 Replies)
Discussion started by: siri_886
4 Replies

7. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

8. Shell Programming and Scripting

Flat File column manipulation

Hi All, I have a tab delimited input file with say 4 fields (columns) as below : 0000443 1AGPR061 2006 Daiml 0002198 1B3XG0K2 1989 Chdds 0002199 1Bd64J0L 1990 Ch34s 0002275 1B3s4J0K 1989 Chadys 0002276 1B465302 2002 Dageml 0002290 1B45430K 1989 Cays I want the 2nd column in file to... (5 Replies)
Discussion started by: net
5 Replies

9. Shell Programming and Scripting

Converting Column to Rows in a Flat file

Hi, Request To guide me in writing a shell program for the following requirement: Example:if the Input File contains the follwing data Input File Data: 80723240029,12,323,443,88,98,7,98,67,87 80723240030,12,56,6,,,3,12,56,6,7,2,3,12,56,6,7,2,3,88,98,7,98,67,87... (5 Replies)
Discussion started by: srinikal
5 Replies

10. Shell Programming and Scripting

Column names in flat files

Hi all, I want to create column names in a flat file and then load the data through some other application. For example, I have a file with emp.txt and I need column names as eno,ename,sal in the first line. The delimiter here is comma and record delimiter is end of line or unix new line. Could... (1 Reply)
Discussion started by: srivsn
1 Replies
Login or Register to Ask a Question