Merging middle column in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging middle column in a file
# 1  
Old 09-02-2016
Merging middle column in a file

I want to merge files as described below:

File 1

Code:
a;1;abc
b;2;def
c;3;xyz
d;4;pqr
e;5;mno


File 2

Code:
b;41
d;77


output

Code:
a;1;abc
b;41;def
c;3;xyz
d;77;pqr
e;5;mno

Please suggest how to do this.

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 09-02-2016 at 05:30 AM.. Reason: Add CODE tags.
# 2  
Old 09-02-2016
Is this a homework assignment? Homework and coursework questions can only be posted in the Homework & Coursework Questions forum as specified in the special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

If this is not a homework item, please explain how this code will be used and tell us what operating system and shell you're using.
# 3  
Old 09-02-2016
No this is not a home work assignment we use flat files to load our user database from multiple locations. Every record contains multiple fields for a user. For a specific set of users, the value coming in File is wrong. For all such users we get another correction file. So for such users we just want to overwrite one field value from correction file before loading it into our database.

32690 pts/2 00:00:00 ksh
# 4  
Old 09-02-2016
Hello Prabal Ghura,

Could you please try following.
Code:
awk -F";" FNR==NR'{A[$1]=$0;next} ($1 in A){print A[$1] FS $NF;next} {print}' Input_file2  Input_file1

Output will be as follows.
Code:
a;1;abc
b;41;def
c;3;xyz
d;77;pqr
e;5;mno

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 09-02-2016
Any attempts/ideas/thoughts from your side?
# 6  
Old 09-02-2016
Howsoever, try also
Code:
awk 'FNR == NR {T[$1] = $2; next} $1 in T {$2 = T[$1]}  1' FS=";" OFS=";" file2 file1

This User Gave Thanks to RudiC For This Post:
# 7  
Old 09-02-2016
Thanks RavinderSingh13, you saved my day
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding new column in the middle

Hi , I need to add few new columns in existing file .Any help would be great ex: existing file name,typ,add,dept New file(com1,sal are new) name,com1,type,sal,add,dept Thanks, mohan (1 Reply)
Discussion started by: mohan705
1 Replies

2. UNIX for Dummies Questions & Answers

File merging based on column patterns

Hello :) I am in this situation: Input: two tab-delimited files, `File1` and `File2`. `File2` (`$2`) has to be parsed by patterns found in `File1` (`$1`). Expected output: tab-delimited file, `File3`. `File3` has to contain the same rows as `File2`, plus the corresponding value in... (5 Replies)
Discussion started by: dovah
5 Replies

3. UNIX for Dummies Questions & Answers

How to generate one long column by merging two separate two columns in a single file?

Dear all, I have a simple question. I have a file like below (separated by tab): col1 col2 col3 col4 col5 col6 col7 21 66745 rs1234 21 rs5678 23334 0.89 21 66745 rs2334 21 rs9978 23334 0.89 21 66745 ... (4 Replies)
Discussion started by: forevertl
4 Replies

4. UNIX for Dummies Questions & Answers

merging rows into new file based on rows and first column

I have 2 files, file01= 7 columns, row unknown (but few) file02= 7 columns, row unknown (but many) now I want to create an output with the first field that is shared in both of them and then subtract the results from the rest of the fields and print there e.g. file 01 James|0|50|25|10|50|30... (1 Reply)
Discussion started by: A-V
1 Replies

5. Shell Programming and Scripting

Merging rows with same column 1 value

I have the following space-delimited input: 1 11.785710 117.857100 1 15 150 1 20 200 1 25 250 3 2.142855 21.428550 3 25 250 22 1.071435 10.714350 The first field is the ID number, the second field is the percentage of the total points that the person has and the third column is the number... (3 Replies)
Discussion started by: mdlloyd7
3 Replies

6. Shell Programming and Scripting

File merging using first column as the ref

I had two files 1.txt 2.txt. I want a 3rd file(o/p) 3.txt like below based on the common elements from the first coulmns of 1.txt and 2.txt. 1.txt 11 12 13 14 15 16 17 18 19 20 21 2.txt (6 Replies)
Discussion started by: p_sai_ias
6 Replies

7. Shell Programming and Scripting

File merging using first column as the ref

I had two files 1.txt 2.txt. I want a 3rd file(o/p) 3.txt like below (using awk) 1.txt 11 a1 12 a2 13 a3 14 a4 15 a5 16 a6 17 a7 18 a8 19 a9 20 a10 2.txt 14 b1 15 b2 16 b3 (8 Replies)
Discussion started by: p_sai_ias
8 Replies

8. UNIX for Dummies Questions & Answers

Merging Tables by a column

Dear Friends, I really do not know Linux and I really would like to understand it because it does help to work with large data. I am reading this forum for 1 week to try a solution for my problem. I think that, using others post informations, I was almost there... I have 2 big tables... (4 Replies)
Discussion started by: lColli
4 Replies

9. Shell Programming and Scripting

Extracting a column from a file and merging with other file using awk

Hi All: I have following files: File 1: <header> text... text .. text .. text .. <\header> x y z ... File 2: <header> text... text .. text .. (4 Replies)
Discussion started by: mrn006
4 Replies

10. Shell Programming and Scripting

Merging column files

Hi,Iam new to Unix.I have a file FileA which is a variable length file where each column is seperated by delimitter "|". FileA: SrNo Name Address 1-234|name1|Addr1 1-34|name2|Addr2 1-2345|name3|Addr3 FileB: SrNo Address 1-34<<06 SPACES>>Addr1<<8 spaces>> 1-234<<05... (1 Reply)
Discussion started by: swapna321
1 Replies
Login or Register to Ask a Question