Sponsored Content
Full Discussion: Joining Two Files Using Awk
Top Forums Shell Programming and Scripting Joining Two Files Using Awk Post 302403988 by Jeetuibm on Monday 15th of March 2010 09:40:15 AM
Old 03-15-2010
Joining Two Files Using Awk

Hi All,
I am new to awk program. But i have got some assignment on awk.

The problem is:

i have two files file1 and file2. Both files have same structure. First i have to join both files on filed1,field2 and field3 and then for matching records i want to perform some calculation like:
Code:
filed_tmp1=file1.filed4-file2.field4
field_tmp2=file1.filed5-file2.field5
field_tmp3=file1.filed6-file2.field6

and output file should be like for matching records

Filed1|Field2|Field3|filed_tmp1|filed_tmp1|filed_tmp1|Field7

and for non-matching records:

FIeld1|Field2|Field3|Field4|Field5|Field6|Field7

all from file1
ex:
File1
Code:
12345|JADE1|1|12|120|234.45|0|0|0|0|0|0
45678|JADE3|1|12|120|234.45|0|0|0|0|0|0
98765|JADE2|1|12|120|234.45|0|0|0|0|0|0

File2

Code:
12345|JADE1|1|6|60|134.45|0|0|0|0|0|0
45678|JADE3|1|5|50|134.45|0|0|0|0|0|0

Outfile:

Code:
12345|JADE1|1|6|60|100|0|0|0|0|0|0
45678|JADE3|1|7|70|100|0|0|0|0|0|0
98765|JADE2|1|12|120|100|0|0|0|0|0|0

I Hope i am clear to all.
Thanks in advance

---------- Post updated at 07:10 PM ---------- Previous update was at 06:01 PM ----------

Code:
nawk -F\| 'BEGIN {
while ((getline < "bcd.txt") > 0)
f2array[$1] = $0
OFS="|"}
{if (f2array[$1])
{
var1=$4
var3=$5
var5=$6
print f2array[$1] > "temp"
getline < "temp"
var2=$4
var4=$5
var6=$6
out1 = var1 - var2
out2 = var3 - var4
out3 = var5 - var6
print $1"|"$2"|"$3"|"out1"|"out2"|"out3"|"$7"|"$8"|"$9"|"$10"|"$11"|"$12
}
else
 print $0
}' abc.temp_tmp


Last edited by pludi; 03-15-2010 at 10:58 AM.. Reason: Please use code tags!!!
 

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Need Help with Joining Command or AWK

Hello. I have 2 files. File 1: 1692 @00000000201@00000205@20090402@000015000 1692 @00000000202@00000206@20090402@000010000 263157760@00000000401@00000201@20090402@000056250 510584583@00000000300@00000207@20090402@000030000 571764164@00000000100@00000128@20090402@000000500... (12 Replies)
Discussion started by: crazyhpux
12 Replies

3. Shell Programming and Scripting

awk joining lines

Hello, I'm trying to write a piece of code in awk, which should be able recognize by some regexps two lines and then join them together (maybe write them without \n would be enough, I don't know).. the thing is that every line in the file i'm working with starts with some number, for example: ... (4 Replies)
Discussion started by: midin
4 Replies

4. Shell Programming and Scripting

Joining 3 AWK scripts to avoid use "temp" files

Hi everyone, Looking for a suggestion to improve the below script in which I´ve been working. The thing is I have 3 separated AWK scripts that I need to apply over the inputfile, and for scripts (2) and (3) I have to use a "temp" file as their inputfile (inputfile_temp and inputfile_temp1... (2 Replies)
Discussion started by: cgkmal
2 Replies

5. 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

6. Shell Programming and Scripting

Help with joining files and adding headers to files

Hi, I have about 20 tab delimited text files that have non sequential numbering such as: UCD2.summary.txt UCD45.summary.txt UCD56.summery.txt The first column of each file has the same number of lines and content. The next 2 column have data points: i.e UCD2.summary.txt: a 8.9 ... (8 Replies)
Discussion started by: rrdavis
8 Replies

7. 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

8. 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

9. Shell Programming and Scripting

Joining files using awk not extracting all columns from File 2

Hello All I'm joining two files using Awk by Left outer join on the file 1 File 1 1 AA 2 BB 3 CC 4 DD File 2 1 IND 100 200 300 2 AUS 400 500 600 5 USA 700 800 900 (18 Replies)
Discussion started by: venkat_reddy
18 Replies

10. UNIX for Beginners Questions & Answers

Issue with awk when joining two files when field has '-' hyphen

Dear Community; I need to join two files but I am facing issues. 1st file has multiple columns. Primary (1st) columns has unique values. There are other columns out of which some has non-ascii characters as well (other language). Example File below: 1-1001JRL,BiRecurring... (5 Replies)
Discussion started by: mystition
5 Replies
JOIN(1) 						      General Commands Manual							   JOIN(1)

NAME
join - relational database operator SYNOPSIS
join [ options ] file1 file2 DESCRIPTION
Join forms, on the standard output, a join of the two relations specified by the lines of file1 and file2. If one of the file names is the standard input is used. File1 and file2 must be sorted in increasing ASCII collating sequence on the fields on which they are to be joined, normally the first in each line. There is one line in the output for each pair of lines in file1 and file2 that have identical join fields. The output line normally con- sists of the common field, then the rest of the line from file1, then the rest of the line from file2. Input fields are normally separated spaces or tabs; output fields by space. In this case, multiple separators count as one, and leading separators are discarded. The following options are recognized, with POSIX syntax. -a n In addition to the normal output, produce a line for each unpairable line in file n, where n is 1 or 2. -v n Like -a, omitting output for paired lines. -e s Replace empty output fields by string s. -1 m -2 m Join on the mth field of file1 or file2. -jn m Archaic equivalent for -n m. -ofields Each output line comprises the designated fields. The comma-separated field designators are either 0, meaning the join field, or have the form n.m, where n is a file number and m is a field number. Archaic usage allows separate arguments for field designators. -tc Use character c as the only separator (tab character) on input and output. Every appearance of c in a line is significant. EXAMPLES
sort /etc/passwd | join -t: -1 1 -a 1 -e "" - bdays Add birthdays to the /etc/passwd file, leaving unknown birthdays empty. The layout of /adm/users is given in passwd(5); bdays con- tains sorted lines like tr : ' ' </etc/passwd | sort -k 3 3 >temp join -1 3 -2 3 -o 1.1,2.1 temp temp | awk '$1 < $2' Print all pairs of users with identical userids. SOURCE
/src/cmd/join.c SEE ALSO
sort(1), comm(1), awk(1) BUGS
With default field separation, the collating sequence is that of sort -b -ky,y; with -t, the sequence is that of sort -tx -ky,y. One of the files must be randomly accessible. JOIN(1)
All times are GMT -4. The time now is 06:14 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy