![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
|||||
|
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 |
|
||||
|
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
|
|
|||||
|
check this post
rearrange file based on lookup file |
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|