Replacing 12 columns of one file by second file based on mapping in third file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing 12 columns of one file by second file based on mapping in third file
# 8  
Old 07-29-2016
In your samples the prod file had a header line. ALL header lines are discarded.

NoF is the sequence No. of the input files. With the first, a field mapping array is filled. With the second, a translation array (indexed by line No. (FNR) and target field No.) is built, i.e. the strings to be filled into the resp. fields in resp. lines. When the third is worked upon, the target fields are being replaced from the beforementioned array, and the lookup file is being written.

Last edited by RudiC; 07-29-2016 at 04:49 PM..
# 9  
Old 07-30-2016
Hi RudiC
Please help me in correcting the lookup file so that order of records appearing in it is same as mapfile .currently the field 'first_name' is appearing as last field in lookup instead of first field as per map file.TIA
# 10  
Old 07-30-2016
So - what did this statement mean?

Quote:
Originally Posted by megh12
.
.
.
The output lookup file data fields can be in any order.
.
.
.

EDIT: Your additional request means a major rewriting of the scriptlet, as (man awk):
Quote:
for ( var in array ) statement

sets var to each index of array and executes statement. The order that var transverses the indices of array is not defined.

Last edited by RudiC; 07-30-2016 at 08:22 AM.. Reason: Added reasoning against rewriting.
# 11  
Old 07-30-2016
okay thankyou
# 12  
Old 07-30-2016
After some cogitating, the rewrite is not that complex, although it needs to set up another, entirely new and sort of independent data structure. Try
Code:
awk -F"|" '
FNR == 1        {NoF++
                 next
                }
NoF == 1        {sub (/ *#.*$/, _)
                 TR1[$1]   = $2
                 SQ[++SMX] = $1
                }
NoF == 2        {for (t in TR1) if (TR1[t]) TR2[FNR,t] = $(TR1[t])
                }

NoF == 3        {for (i=1; i<=SMX; i++) printf "%s|%s|", $SQ[i], TR2[FNR,SQ[i]] > LOF
                 printf RS > LOF
                 for (t in TR1)         if (TR1[t]) $t = TR2[FNR,t]
                 print
                }

' mapfile mockfile OFS="|" LOF="lookupfile" prodfile
value a|100000|Meena|Kumari|Meena Kumari|02/02/1981|value b|value c |ABCDEF1232F|value d
value a1|100003|Dhyan|Chand|Dhyan Chand|02/02/1982|value b1|value c1|ABCD4567M|value d1
cat lookupfile 
vijayendra|Meena|kumar|Kumari|vijayendra kumar|Meena Kumari|10/101984|02/02/1981|AOYUGH9282P|ABCDEF1232F|100000||
ravi|Dhyan|kumar|Chand|ravi kumar|Dhyan Chand|01/01/1987|02/02/1982|AOJKUYT0908P|ABCD4567M|100003||

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find columns in a file based on header and print to new file

Hello, I have to fish out some specific columns from a file based on the header value. I have the list of columns I need in a different file. I thought I could read in the list of headers I need, # file with header names of required columns in required order headers_file=$2 # read contents... (11 Replies)
Discussion started by: LMHmedchem
11 Replies

2. Shell Programming and Scripting

Help with awk replacing identical columns based on another file

Hello, I am using Awk in UBUNTU 12.04. I have a file like following with three fields and 44706 rows. F1 A A F2 G G F3 A T I have another file like this: AL_1 F1 A A AL_2 F1 A T AL_3 F1 A A AL_1 F2 G G AL_2 F2 G A AL_3 F2 G G BO_1 F1 A A BO_2 F1 A T... (6 Replies)
Discussion started by: Homa
6 Replies

3. Shell Programming and Scripting

Search and replace with mapping from a mapper file in a target file

Hello, I have a special problem. I have a file in 8 bit and would like to convert the whole database to 16Bit unicode. The mapping file has the following structure: The mapper is provided as a zip file The target file to be converted contains data in English and 8 bit Urdu mapping, a... (4 Replies)
Discussion started by: gimley
4 Replies

4. Shell Programming and Scripting

Filtering first file columns based on second file column

Hi friends, I have one file like below. (.csv type) SNo,data1,data2 1,1,2 2,2,3 3,3,2 and another file like below. Exclude data1 where Exclude should be treated as column name in file2. I want the output shown below. SNo,data2 1,2 2,3 3,2 Where my data1 column got removed from... (2 Replies)
Discussion started by: ks_reddy
2 Replies

5. Shell Programming and Scripting

Finding/replacing strings in some files based on a file

Hi, We have a file (e.g. a .csv file, but could be any other format), with 2 columns: the old value and the new value. We need to modify all the files within the current directory (including subdirectories), so find and replace the contents found in the first column within the file, with the... (9 Replies)
Discussion started by: Talkabout
9 Replies

6. Shell Programming and Scripting

Replacing headers based on a second file

I have a file with thousands of sequences that looks like this: I need to replace the headers using a second file Thus, I will end up having the following file: I am looking for an AWK script that I can easily plug in my current pipeline. Any help will be greatly appreciated! (6 Replies)
Discussion started by: Xterra
6 Replies

7. UNIX for Dummies Questions & Answers

Script for replacing text in a file based on list

Hi All, I am fairly new to the world of Unix, and I am looking for a way to replace a line of text in a file with a delimited array of values. I have an aliases file that is currently in use on our mail server that we are migrating off of. Until the migration is complete, the server must stay... (8 Replies)
Discussion started by: phoenixjc
8 Replies

8. Shell Programming and Scripting

Replacing Character in a file based on element

Hi, I have file like below. Unix:/pclls/turc>cat tibc.property executeReceiver=Y executeSender=Y I want to replace executeSender=N in the file. My file should be like below. executeReceiver=Y executeSender=N I tried with the below command, its giving error. cat tibc.property |... (2 Replies)
Discussion started by: senthil_is
2 Replies

9. Shell Programming and Scripting

sorting file based on two or more columns

Hi gang. I'm using a unix/mac system and i'm trying to sort a file (more than 1,000,000 lines). chr1 100000965 100001001 - chr1 100002155 100002191 + chr1 100002165 100002201 + chr1 100002525 100002561 - chr1 10000364 ... (2 Replies)
Discussion started by: labrazil
2 Replies

10. Shell Programming and Scripting

Replacing columns into another file

Hi, I have input file. File1: Seqno Name 121 name1 122 name2 123 name3 124 name4 We will send the file1 to some other team. They will replace name column with place in file1 and send back to us as file2. file2: Seqno Place 121 place1 122 place2 124 place3 (2 Replies)
Discussion started by: manneni prakash
2 Replies
Login or Register to Ask a Question