Merging middle column in a file


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

---------- Post updated at 02:59 PM ---------- Previous update was at 02:58 PM ----------

Thanks RavinderSingh13

---------- Post updated at 03:44 PM ---------- Previous update was at 02:59 PM ----------

RavinderSingh13
A quick question what if we use different delimiters in both files?
# 9  
Old 09-02-2016
Quote:
Originally Posted by Prabal Ghura
RavinderSingh13
A quick question what if we use different delimiters in both files?
Hello Prabal Ghura,

So let's say your 1 file named Input_file2 has delimiter as ; and Input_file1 has delimiter as ,, we could set it for both of them. This is just an example, you could try to fine tune it as per your requirements too.
Code:
awk  FNR==NR'{A[$1]=$0;next} ($1 in A){print A[$1] FS $NF;next} {print}' FS=";" Input_file2  FS="," Input_file1

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 10  
Old 09-02-2016
Then change the field separator value before reading each file:
Code:
awk 'FNR==NR{A[$1]=$2;next} $1 in A{$2=A[$1]} 1' FS='x' Input_file2  FS='y' OFS='y' Input_file1

where x is the field separator in Input_file2 and y is the field separator in Input_file1 (and the output).

If you want a different field separator in the output than you are using as the File 1 input field separator, it is a little more complicated:
Code:
awk 'FNR==NR{A[$1]=$2;next} $1 in A{$2=A[$1]} {$1=$1} 1' FS='x' Input_file2  FS='y' OFS='z' Input_file1

where z is the field separator you want in the output file.
This User Gave Thanks to Don Cragun For This Post:
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