Join file contents via common field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Join file contents via common field
# 1  
Old 05-09-2008
Join file contents via common field

I have 2 files with a common parm - Jobname

File 1
0507 1202 JOBA
0507 1302 JOBB
0507 1452 JOBC
0507 1552 JOBA
0507 1553 JOBA

File2

JOBA abcdefg server4
JOBB defghij server22
JOBC vwxyz12 server55

I would like to take each line from File1 and match the jobname with the jobname in File 2 and produce File 3 as

0507 1202 JOBA abcdefg server4
0507 1302 JOBB defghij server22
0507 1452 JOBC vwxyz12 server55
0507 1552 JOBA abcdefg server4
0507 1553 JOBA abcdefg server4

Could anyone help please, I'm new to scripting.
# 2  
Old 05-09-2008
#!/bin/ksh

cat file1.txt | while read line
do
JOBNAME=$(echo ${line} | awk '{print $3}')
LINE2=$(grep -w $JOBNAME file2.txt)
echo "$line $LINE2" >> file3.txt
done

I think it 's OK
# 3  
Old 05-09-2008
Got the following result

cat file1.txt | while read line
while: Expression syntax.

from

#!/bin/ksh

cat file1.txt | while read line
do
JOBNAME=$(echo ${line} | awk '{print $3}')
LINE2=$(grep -w $JOBNAME file2.txt)
echo "$line $LINE2" >> file3.txt
done
# 4  
Old 05-09-2008
#!/bin/ksh

cat file1.txt | while read line
do
JOBNAME=$(echo ${line} | awk '{print $3}')
LINE2=$(grep -w $JOBNAME file2.txt | awk '{print $2 " " $3}')
echo "$line $LINE2" >> file3.txt
done

it 's better ...
# 5  
Old 05-09-2008
Quote:
Originally Posted by Northerner
Got the following result

cat file1.txt | while read line
while: Expression syntax.

from

#!/bin/ksh

cat file1.txt | while read line
do
JOBNAME=$(echo ${line} | awk '{print $3}')
LINE2=$(grep -w $JOBNAME file2.txt)
echo "$line $LINE2" >> file3.txt
done

You have to add the full path and the right name of your file.
For exemple a file in /tmp :
cat /tmp/File1 | while read line
do
JOBNAME=$(echo ${line} | awk '{print $3}')
LINE2=$(grep -w $JOBNAME /tmp/File2 | awk '{print $2 " " $3}')
echo "$line $LINE2" >> /tmp/File3
done
# 6  
Old 05-09-2008
I'm sorry I get the same result with full path specified.
# 7  
Old 05-09-2008
it ' s amaising !

On my server (AIX), I have created 4 files :
File1 with the following content :
0507 1202 JOBA
0507 1302 JOBB
0507 1452 JOBC
0507 1552 JOBA
0507 1553 JOBA

File2 :
JOBA abcdefg server4
JOBB defghij server22
JOBC vwxyz12 server55

My script newscript.ksh (chmod u+x newscript.ksh) :
#!/bin/ksh

cat File1 | while read line
do
JOBNAME=$(echo ${line} | awk '{print $3}')
LINE2=$(grep -w $JOBNAME File2 | awk '{print $2 " " $3}')
echo "$line $LINE2" >> File3
done


I'm running the script $PWD/newscript.ksh

So, I have a new file File3 :
# more File3
0507 1202 JOBA abcdefg server4
0507 1302 JOBB defghij server22
0507 1452 JOBC vwxyz12 server55
0507 1552 JOBA abcdefg server4
0507 1553 JOBA abcdefg server4

It's working fine for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to change contents of field based on condition in same file

In the awk below I am trying to copy the entire contents of $6 there may be multiple values seperated by a ;, to $8, if $8 is . (lines 1 and 3 are examples). If that condition $8 is not . (line2 is an example) then that line is skipped and printed as is. The awk does execute but prints the output... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

awk to update value in field of out file using contents of another Ask

In the out.txt below I am trying to use awk to update the contents of $9.. If $9 contains a + or - then $8 of out.txt is used as a key to lookup in $2 of file. When a match ( there will always be one) is found the $3 value of that file is used to update $9 of out.txt separated by a :. So the... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

If contents of A are in B then move the common contents to C

Hallo Team, I have 2 .csv files file A has 47600 lines and file B has 67000 lines FILEA SD0o9rb01-1d320ddbcc8d220f572739ebed5f58d1-v300g00 SD8bt0101-a0810bfe0e3396060126ec51b30dac0a-v300g00 SD05sce01-cb056af347ed4651f29eb3c3e9addbd6-v300g00... (3 Replies)
Discussion started by: kekanap
3 Replies

4. Shell Programming and Scripting

Merging files with common IDs without JOIN

Hi, I am trying to merge information across 2 files. The first file is a "master" file, with all IDS. File 2 contains a subset of IDs of those in File 1. I would like to match up individuals in File 1 and File 2, and add information in File 2 to that of File 1 if they appear. However, if an... (3 Replies)
Discussion started by: hubleo
3 Replies

5. Shell Programming and Scripting

Join two files with common and range identifiers

I have a problem joining two files. The first file abc.txt has 10k lines and has lots of fields but two fields fff1 and ppp1 to merge by. The second file xyz.txt is a master file with 1k lines and lots of fields but three fields to merge by fff1; rrr1 and qqq1. The two files need to be merged... (9 Replies)
Discussion started by: cfiles2012
9 Replies

6. UNIX for Dummies Questions & Answers

How to use the the join command to join multiple files by a common column

Hi, I have 20 tab delimited text files that have a common column (column 1). The files are named GSM1.txt through GSM20.txt. Each file has 3 columns (2 other columns in addition to the first common column). I want to write a script to join the files by the first common column so that in the... (5 Replies)
Discussion started by: evelibertine
5 Replies

7. Shell Programming and Scripting

Compare a common field in two files and append a column from File 1 in File2

Hi Friends, I am new to Shell Scripting and need your help in the below situation. - I have two files (File 1 and File 2) and the contents of the files are mentioned below. - "Application handle" is the common field in both the files. (NOTE :- PLEASE REFER TO THE ATTACHMENT "Compare files... (2 Replies)
Discussion started by: Santoshbn
2 Replies

8. UNIX for Dummies Questions & Answers

how to join two files using "Join" command with one common field in this problem?

file1: Toronto:12439755:1076359:July 1, 1867:6 Quebec City:7560592:1542056:July 1, 1867:5 Halifax:938134:55284:July 1, 1867:4 Fredericton:751400:72908:July 1, 1867:3 Winnipeg:1170300:647797:July 15, 1870:7 Victoria:4168123:944735:July 20, 1871:10 Charlottetown:137900:5660:July 1, 1873:2... (2 Replies)
Discussion started by: mindfreak
2 Replies

9. Shell Programming and Scripting

join files based on a common field

Hi experts, Would you please help me with this? I have several files and I need to join the forth field of them based on the common first field. here's an example... first file: 280346 39.88 -75.08 547.8 280690 39.23 -74.83 538.7 280729 40.83 -75.08 499.2 280907 40.9 -74.4 507.8... (5 Replies)
Discussion started by: GoldenFire
5 Replies

10. Shell Programming and Scripting

Remove spaces from first field, and write entire contents into other text file

Hi all, I have searched and found various threads about removing spaces from a field within a text file. Unfortunately, I have not found exactly what I'm looking for, nor am I adept enough to modify what I've found into what I need. I use the following command to remove the first line... (3 Replies)
Discussion started by: carriehoff
3 Replies
Login or Register to Ask a Question