Joining


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Joining
# 1  
Old 04-23-2013
Joining

I do have many files of the following format.

File_1.txt
Code:
1 24
2 25
3 27
4 28
5 29
6 1

File_2.txt
Code:
2 5
3 8
4 9
5 10

File_3.txt
Code:
3 7
4 8
5 9

I wanted to merge all these files based on the first column like this:
Code:
1 24 0 0
2 25 5 0
3 27 8 7
4 28 9 8
5 29 10 9
6 1 0 0

Basically merge it based on the first column and add 0's at the missing positions.
I tried:
Code:
awk  '{i=$1;sub(i,x);A[i]=A[i]$0} FILENAME==ARGV[ARGC-1]{print i A[i]}' File_1.txt File_2.txt File_3.txt

But the missing values are ignored and no 0's were added.

Let me know the best way to do this using awk.
# 2  
Old 04-23-2013
You could use join command:
Code:
join -a 1 -a 2 -e '0' -o 1.1 1.2 2.2 file1 file2 > tmp
join -a 1 -a 2 -e '0' -o 1.1 1.2 1.3 2.2 tmp file3

---------- Post updated at 12:28 ---------- Previous update was at 12:16 ----------

Here is an approach using awk based on some assumptions:
Code:
awk '
        BEGIN {
                F = "file1"
                while ( (getline line < F) > 0 )
                {
                        split (line, A)
                        F1[++c] = A[2]
                }
                close (F)
                F = "file2"
                while ( (getline line < F) > 0 )
                {
                        split (line, A)
                        F2[A[1]] = A[2]
                }
                close (F)
                F = "file3"
                while ( (getline line < F) > 0 )
                {
                        split (line, A)
                        F3[A[1]] = A[2]
                }
                close (F)
        }
        END {
                for ( k = 1; k <= c; k++ )
                        print k, F1[k], F2[k]?F2[k]:"0", F3[k]?F3[k]:"0"
        }
' /dev/null

# 3  
Old 04-23-2013
Thanks, but it won't help. I have many files to join
# 4  
Old 04-23-2013
Code:
awk '

F != FILENAME { FILENO++ ; F=FILENAME }
{ A[$1,FILENO]=$2 }
!($1 in C) { C[$1]++; COL[++L]=$1 }

END {
        for(N=1; N<=L; N++)
        {
                printf("%d", COL[N]+0);
                for(FILE=1; FILE<=FILENO; F++)
                        printf(" %d", A[COL[N],FILE]+0);
                printf("\n");
        }
}' file1 file2 file3 ...

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Please help me in joining two files

I have two files with the below contents : sampleoutput3.txt 20150202;hostname1 20150223;hostname2 20150716;hostname3 sampleoutput1.txt hostname;packages_out_of_date;errata_out_of_date; hostname1;11;0; hostnamea;12;0; hostnameb;11;0; hostnamec;95;38; hostnamed;440;358;... (2 Replies)
Discussion started by: rahul2662
2 Replies

2. Shell Programming and Scripting

Joining lines in different way

Hi all, I'm excited to the part of unix.com forum, and noob to it. I have an query, where I have an file and it contains data like this use thread when posting do no I was expecting the result as use thread thread when when posting posting do do no use thread when thread when... (6 Replies)
Discussion started by: Jose Nirmal
6 Replies

3. Shell Programming and Scripting

Joining 2 Files

File "A" (column names: Nickname Number GB) Nickname Number GB PROD_DB0034 100A 16 ASMIL1B_DATA_003 100B 16 PSPROD_0000 1014 36 PSPROD_0001 100D 223 ..... File "B" (column names: TYPE DEVICE NUMBER SIZE) TYPE DEVICE NUMBER SIZE 1750500 hdisk2 100A 16384 1750500 hdisk3 ... (4 Replies)
Discussion started by: Daniel Gate
4 Replies

4. Shell Programming and Scripting

Joining Three Files

Hi guys, I have three files which needs to be joined to a single file. File 1: Col a, Col b, Col c File 2: Col 1a, Col 1b File 3: Col 2a, Col 2b Output: Col 1a, Col 2a, Col a, Col b, Col c. All the files are comma delimited. I need to join Col b with Col 1b and need to... (17 Replies)
Discussion started by: mac4rfree
17 Replies

5. Shell Programming and Scripting

variable joining

I really need help on this problem. The story: My VAR1 and VAR2 works fine and able to get the value. I want to do as "if VAR1 or VAR2 is bigger than max_loadavg then the code will run...then if VAR1 or VAR2 is lesser than min_loadavg then the other code will run... The problem: The... (8 Replies)
Discussion started by: hezry79
8 Replies

6. Shell Programming and Scripting

Need help joining lines

Hi All, I need the command to join 2 lines into one. I found lots of threads but none give me the sollution. Probably because unix scripting is one of my best features ;) I got a logfile where line 2 needs to be joined with line 1, lines 4 needs to be joined with line 3 etc If you need... (16 Replies)
Discussion started by: rene21976
16 Replies

7. Shell Programming and Scripting

Joining of Commands.

Hello, i have a few questions which i hope you guys can address. the 1st prob: The database i am using is a text file which displays the information like this : Title : Quantity : Price Persia : 30 : 20 ... (5 Replies)
Discussion started by: gregarion
5 Replies

8. Shell Programming and Scripting

Joining Variables

Hi Everyone, Thanks in advance for what is hopefully a simple thing. Big picture I am trying to bind computers to AD using their existing name but add a suffix. Here is the part I am having trouble with: computername=`/usr/sbin/scutil --get LocalHostName` suffix=suffix... (2 Replies)
Discussion started by: dcc186
2 Replies

9. Shell Programming and Scripting

Help with joining two files

Greetings, all. I've got a project that requires I join two data files together, then do some processing and output. Everything must be done in a shell script, using standard unix tools. The files look like the following: File_1 Layout: Acct#,Subacct#,Descrip Sample: ... (3 Replies)
Discussion started by: rjlohman
3 Replies

10. UNIX for Dummies Questions & Answers

joining 2 files

Hi, I have two files that I need to find difference between. Do I use diff or join? If join, how do I use it? thanks, webtekie (1 Reply)
Discussion started by: webtekie
1 Replies
Login or Register to Ask a Question