To find and merge two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To find and merge two files
# 1  
Old 09-03-2009
To find and merge two files

Hi All,

I have two files file1 and file2 as below.

cat file1

200000040;20.0
200000020;40.0
200000060;10
200000100;.0.5
----------------
cat file2

200000020
200000100
200000040
200000060
I want to merge this two files by comparing the field values of the first column of both the files. The merger file should be like this.

cat merged_file

200000020 ;40.0
200000100;0.5
200000040;20.0
200000060;10

Kindly suggest how to do this.

thanks in advance,
Giri.
# 2  
Old 09-03-2009
Try...

Code:
 
awk -F";" 'NR==FNR{a[$1]}NR!=FNR{for(i in a)if(i==$1) print $0}' file2 file1

# 3  
Old 09-03-2009
With grep:

Code:
grep -f fi1e2 file1

or with another awk solution:

Code:
awk -F";" 'NR==FNR{a[$1];next}$1 in a' file2 file1

# 4  
Old 09-03-2009
With Perl:

Code:
perl -F\; -lane'
  next if @F > 1 and $_{$F[0]} = $_;
  print $_{$_};
  ' file1 file2

# 5  
Old 09-03-2009
Thanks a lot to all.... It serves my requiment...

Thanks,
Giri
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find matched patterns in a column of 2 files with different size and merge them

Hi, i have input files like below:- input1 Name Seq_ID NewID Scores MT1 A0QZX3 1.65 277.4 IVO A0QZX3 1.65 244.5 HPO A0QZX3 1.65 240.5 RgP A0Q3PP 5.32 241.0 GX1 LPSZ3S 96.1 216.9 MEL LPSS3X 4.23 204.1 LDD LPSS3X 4.23 100.2 input2 Fac AddName NewID ... (9 Replies)
Discussion started by: redse171
9 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. Shell Programming and Scripting

How to merge two files?

Hi Gurus, I have two files as below file1 abc cde cdd cdf file2 123 234 345 456 I want to get abc 123 cde 234 cdd 345 (3 Replies)
Discussion started by: ken6503
3 Replies

4. Shell Programming and Scripting

Merge files

a.txt id name subject 12 aaa History 23 bbb Science 45 ccc Zoology b.txt id layer LayerNo 12 xxx12 1 23 yyy23 2 23 lll23 3 45 xxx45 1 45 yyy45 2 45 lll45 3 i have file a.txt which is parent file and another children file b.txt . Both files are linked together by common field... (2 Replies)
Discussion started by: manas_ranjan
2 Replies

5. Shell Programming and Scripting

Find duplicates in column 1 and merge their lines (awk?)

Hi, I have a file (sorted by sort) with 8 tab delimited columns. The first column contains duplicated fields and I need to merge all these identical lines. My input file: comp100002 aaa bbb ccc ddd eee fff ggg comp100003 aba aba aba aba aba aba aba comp100003 fff fff fff fff fff fff fff... (5 Replies)
Discussion started by: falcox
5 Replies

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

7. Shell Programming and Scripting

How can i merge these two files into several...

Given are File A and File B File A has for example 5 lines: AAA BBB CCC DDD EEE File B has 3 lines: 111 222 333 How can i merge A and B into: 111 222 333 AAA (first line from A) then a new file: (4 Replies)
Discussion started by: Y-T
4 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. Shell Programming and Scripting

help in merge files

I have created these files in a particular directory a_1.txt a_2.txt ... .. a_n.txt Each file has a single line. I want to write a output file a.txt, which will have concantated value of string from all the files. which utility should i use. copy, cat or paste???? Can anyone help... (5 Replies)
Discussion started by: u263066
5 Replies

10. UNIX for Dummies Questions & Answers

Find and Merge files

Hi, I am relatively new to unix and I need to write a simple script to monitor a log directory. Being adventurous, I intend to pick up a couple of scripting to make my work more efficient. I need to 1) merge files with names that starts with the same date Example: 12052005.2alarm ... (2 Replies)
Discussion started by: seongyin
2 Replies
Login or Register to Ask a Question