Intelligently merge two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Intelligently merge two files
# 1  
Old 08-19-2015
Intelligently merge two files

I have two files as the following:

file1:
Code:
243
242
243
242
242

file2:
Code:
243          car       360    396k
242          bike       240    217k

I want the corresponding $3 in file 2 added as $2 in file 2.

The expected output would be:
Code:
243 360
242 240
243 360
242 240
242 240

I figured that I can do this through two loops, but I am not sure how to extract the exact values that I would need, here is that I was thinking of:
Code:
 while read y; do
                while read z; do

                done<file1
        done<file2

Is there a better way to do this using only awk or sed?

Thanks.

---------- Post updated at 11:31 AM ---------- Previous update was at 11:18 AM ----------

I think I figured out a bash based method. I can find matches now. Figuring out a way to print should be straight forward.

Code:
while read y; do
                while read z; do
                        a=$(echo $y | awk '{print $1}')
                        if [ "$a" = "$z" ]; then
                                echo $y
                        else
                                echo nomatch
                        fi
                done<file1
        done<file2

# 2  
Old 08-19-2015
Hello Jamie_123,

You could try following also, which may help you in same.
Code:
 awk 'FNR==NR{X[$1]=$3;next} ($1 in X){print $1 OFS X[$1]}' file2 file1

Output will be as follows.
Code:
243 360
242 240
243 360
242 240
242 240

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 08-19-2015
Code:
awk 'NR == FNR {a[$1] = $3; next} {print $1, (a[$1] == "" ? "nomatch" : a[$1])}' file2 file1

This User Gave Thanks to SriniShoo 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

How to merge two files?

Dear Frens, I have two files and need to merge into one file. Like File_1: Field1 Field2 1 4 File_2: Field1 Field2 3 5 I need one single output as File_1: Field1 Field2 1 4 3 5 This means taking header from either file. (8 Replies)
Discussion started by: manisha_singh
8 Replies

2. Shell Programming and Scripting

Merge files and generate a resume in two files

Dear Gents, Please I need your help... I need small script :) to do the following. I have a thousand of files in a folder produced daily. I need first to merge all files called. txt (0009.txt, 0010.txt, 0011.txt) and and to output a resume of all information on 2 separate files in csv... (14 Replies)
Discussion started by: jiam912
14 Replies

3. UNIX for Dummies Questions & Answers

How to use a error log file intelligently.?

This has broad implications but is there a way to tell which applications are logging into lets say xsessons error file? How do I tell which error messages pertain to which applications? Thanks in advance!:D (2 Replies)
Discussion started by: theKbStockpiler
2 Replies

4. Shell Programming and Scripting

Checking in a directory how many files are present and basing on that merge all the files

Hi, My requirement is,there is a directory location like: :camp/current/ In this location there can be different flat files that are generated in a single day with same header and the data will be different, differentiated by timestamp, so i need to verify how many files are generated... (10 Replies)
Discussion started by: srikanth_sagi
10 Replies

5. UNIX for Dummies Questions & Answers

Merge files

Hi, I would like to know how can I merge files based on their coordinates, but mantaining the score of each file in the output file like: Note: 1st column is for chromosome, 2nd for start, 3rd for end of segment, 4th for score file1: 1 200 300 20 1 400 500 30 file2: 1 200 350 30 1... (1 Reply)
Discussion started by: fadista
1 Replies

6. Shell Programming and Scripting

awk "intelligently" paste two files together.

Hi everyone, I am trying to join two files together based on a field present in both files but not necessarily in the same order. File 1: 123 user1 245 user2 559 user3 File 2: 123 user1 246 user4 544 user2 Also, sometimes a user may not even be in both files. What I want awk to... (3 Replies)
Discussion started by: collards
3 Replies

7. Shell Programming and Scripting

merge two different files

HI I have input file as date 22jan2011 calc 0 667788.1 998877.2 vals 1 222 444 666 777 999 vals 2 222 444 666 777 999 vals 3 ... (3 Replies)
Discussion started by: Indra2011
3 Replies

8. Shell Programming and Scripting

Merge files of differrent size with one field common in both files using awk

hi, i am facing a problem in merging two files using awk, the problem is as stated below, file1: A|B|C|D|E|F|G|H|I|1 M|N|O|P|Q|R|S|T|U|2 AA|BB|CC|DD|EE|FF|GG|HH|II|1 .... .... .... file2 : 1|Mn|op|qr (2 Replies)
Discussion started by: shashi1982
2 Replies

9. UNIX for Advanced & Expert Users

Intelligently Installing from Source

Hi: (I apologise in advance for the lengthy prelude--but i think it's necessary to properly ask my question.) I code in Ruby and Python on Mac OS X. Ruby, for instance, as i suspect everyone here knows, is both open source and it it lacks native support for e.g., GUI and Image Manip. These two... (0 Replies)
Discussion started by: Alex_Land
0 Replies

10. Shell Programming and Scripting

How to merge files

Hello guys, I gotta question, i have a lot of log files (simple text) and i need to merge them in group of 10 files, one next to the other, that have sense? For example, i have the files: File1 File2 File3 File4 . . File100 I need to merge the contents of each file into a new file... (3 Replies)
Discussion started by: lestat_ecuador
3 Replies
Login or Register to Ask a Question