Merging two tab separated files via nawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging two tab separated files via nawk
# 1  
Old 10-10-2012
Merging two tab separated files via nawk

I searched a lot considering this theme,but still cant make my code working.
I have two tab separated files, I want to do the following thing:

File 1:

Code:
xx1 y yy
xx2 y yy
xx3 y yy
xx4 y yy


File 2:

Code:
xx1 z1
xx2 z2
xx3 z3
xx4 z4
xx5 z5

So I want to merge them ,according to the first column and receive:

File 3:

Code:
xx1 y yy z1
xx2 y yy z2
xx3 y yy z3
xx4 y yy z4


nawk with while and getline doesnt work at all, so I tried to write smth with NR,FNR
Code:
 
NR==FNR{a[$1]=$2;next}
{($1 in a) print $1,$4,a[$3]}' file1 file2

the problem is that after {a[$1]=$0 ; next} it doesnt see this array anymore.So after next and } i can not print it and do anything else with it .

I tried to write code like this:
Code:
awk '
NR==FNR{a[$1]=$2;next}
a[$3]{print $1,$4,a[$3]}' file1 file2

but still experienced same problem
Smilie

Thank you for any help!

Last edited by Corona688; 10-10-2012 at 12:26 PM..
# 2  
Old 10-10-2012
have you tried using join...?


Code:
join file1 file2


and with awkSmilie

Code:
awk 'FNR==NR{a[$1]=$0;next}{if(a[$1]){print a[$1],$2}}' file1 file2

# 3  
Old 10-10-2012
I was thinking about this ,but will it be working if I have different amount of rows in files?
# 4  
Old 10-10-2012
Quote:
Originally Posted by divergenciya
I was thinking about this ,but will it be working if I have different amount of rows in files?
As per your requirement it should work.

Try with awk also i have give in previous script..Smilie
# 5  
Old 10-11-2012
Thanks for your reply!
I tried to use awk,but it still doesnt give me any output ,but these files definitely have compatible rows.
Could you please describe me how can I use join command?
# 6  
Old 10-11-2012
Quote:
Originally Posted by divergenciya
I tried to use awk,but it still doesnt give me any output ,but these files definitely have compatible rows.
Could you please describe me how can I use join command?
As per your given input awk and join are working fine..Smilie
Or give some more input. And for awk have you tried with nawk...?

You can use join. Go through man pages of join

Join normally works as to join files. It joins files field by field. You can change the pattern how to join the files. You can get more details in Man pages.

Hope this helps you.Smilie
# 7  
Old 10-11-2012
I sorted files and then used join -1 1 -2 1 file1 fil2 but it gives me zero output
I tried the following code:
Code:
nawk -F\t 'NR==FNR{a[$1]=$0;next} ($1 in a) 'file1 file2

it works almost well,but it doesnt give me the value of the column according to which Im sorting
So it gives the following result
file 1:
Code:
xx1 y1 y2
xx2 y1 y2
xx3  y1 y2

file 2
Code:
xx1 z1 
xx2 z2
xx3 z3 
xx4 z4
xx5 z5

and as output Im receiving
Code:
z1 z2 z3

and I cant figure out how to make it printing at least the first column
its strange that if I add if statement it doesnt see array values anymore

Last edited by Franklin52; 10-11-2012 at 06:00 AM.. Reason: Please use code tags for data and code samples
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Gawk output separated by tab

In the gawk below, I am trying to output the file tab-deliminated but don't think that is the correct syntax. Thank you :). gawk OFS='/t' '{sub(/-+/,"",$2); ar=$0} END{n = asort(ar) for (i = 1; i <= n; i++) print ar}' file (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Merging two special character separated files based on pattern matching

Hi. I have 2 files of below format. File1 AA~1~STEVE~3.1~4.1~5.1 AA~2~DANIEL~3.2~4.2~5.2 BB~3~STEVE~3.3~4.3~5.3 BB~4~TIM~3.4~4.4~5.4 File 2 AA~STEVE~AA STEVE WORKS at AUTO COMPANY AA~DANIEL~AA DANIEL IS A ELECTRICIAN BB~STEVE~BB STEVE IS A COOK I want to match 1st and 3rd... (2 Replies)
Discussion started by: crypto87
2 Replies

3. Shell Programming and Scripting

How to replace & with and in tab separated file?

Hi, I have a tab separated. I want to replace all the "&" in 8th column of the file with "and" .I am trying with awk -F, -vOFS=\\t '{$8=($8=="&")?"and":$8}1' test> test1.txt My file is abc def ghk hjk lkm hgb jkluy acvf & bhj hihuhu fgg me mine he her go went has has & had hgf hgy ... (1 Reply)
Discussion started by: jagdishrout
1 Replies

4. Shell Programming and Scripting

Problem with a tab separated file

Hi, I have created a tab separated file from the following input file. ADDRESS1 CITY STATE POSTAL COUNTRY LON LAT 32 PRINZREGENTENSTRASSE ROSENHEIM BAYERN 83022 DEU 1212182 4785699 263 VIA DANTE ALIGHIERI BARI PUGLIA 70122 ITA 1686233 4112154 30 VIA MILANO ... (1 Reply)
Discussion started by: ramky79
1 Replies

5. Shell Programming and Scripting

replace row separated by tab

Dear users, I have this problem, this is the example: 123 (tab) A (tab) B (tab) C (tab) 456 where the (tab) is actually the \t delimiter. I need to replace the A B and C for D E and F, this is: 123 (tab) D (tab) E (tab) F (tab) 456 The thing is that my file is quite long and this... (2 Replies)
Discussion started by: Gery
2 Replies

6. UNIX for Dummies Questions & Answers

tab-separated file to matrix conversion

hello all, i have an input file like that A A X0 A B X1 A C X2 ... A Z Xx B A X1 B B X3 .... Z A Xx Z B X4 and i want to have an output like that A B C D A X0 X1 X2 Xy B X1 X3 X4 (4 Replies)
Discussion started by: TheTransporter
4 Replies

7. Shell Programming and Scripting

Convert a tab separated file using bash

Dear all, I have a file in this format (like a matrix) - A B C .. X A 1 4 2 .. 2 B 2 6 4 .. 8 C 3 5 5 .. 4 . . . ... . X . . ... . and want to convert it into a file with this format: A A = 1 A B = 4 A C = 2 ... A X = 2 B A = 2 B B = 6 etc (2 Replies)
Discussion started by: TheTransporter
2 Replies

8. Shell Programming and Scripting

Compare two columns separated by a tab

witam potrzebuje polecenia porownujacego koumny na podstawie n-ostatnich znakow danej linnijki tj mam 2 koumny AiB zawierajace ciag dowolnych znakow (dlugosci w kazdej linijce mga byc rozne wiec uzycie substra odpada) A B ewewewabc nbgujnnabc... (3 Replies)
Discussion started by: Toudi
3 Replies

9. Shell Programming and Scripting

Merging files into a single tab delimited file with a space separating

I have a folder that contains say 50 files in a sequential order: cdf_1.txt cdf_2.txt cdf_3.txt cdf_3.txt . . . cdf_50.txt. I need to merge these files in the same order into a single tab delimited file. I used the following shell script: for x in {1..50}; do cat cdf_${x}.txt >>... (3 Replies)
Discussion started by: Lucky Ali
3 Replies

10. Shell Programming and Scripting

parse file into tab separated columns

Hello, I am trying to parse a file that resembles the last three groupings into something looking like the first two lines. I've fiddled with sed and awk a bit, but can't get anything to work properly. I need them separated by some delimiter. The file is some 23,000 lines of the stuff.... ... (9 Replies)
Discussion started by: dkozel
9 Replies
Login or Register to Ask a Question