Joining two files by retaining the headers.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Joining two files by retaining the headers.
# 1  
Old 11-19-2013
Joining two files by retaining the headers.

Hi Experts,

I want to join two files(file1 ,file2) which are having tab separated values,by sorting them on key column(ID) and want to redirect the output to other file(output file) along with the headers from both the files.
Code:
cat file1:
ID	Name	phone_no
205	mno	90808
200	xyz	32003
100	abc	55555

cat file2:
Code:
ID	city	email
205	bangalore	blr@abc.com
100	delhi	dlh@abc.com

Required output:
Code:
ID	Name	phone_no	city	email
205	mno	90808	bangalore	blr@abc.com
100	abc	55555	delhi	dlh@abc.com

Need your help experts

Last edited by bharathbangalor; 11-19-2013 at 07:27 AM..
# 2  
Old 11-19-2013
Try :

Code:
$ awk 'FNR==NR{A[$1]=$2 FS $3;next}($1 in A){$0=$0 FS A[$1]}A[$1]' file2 file1
ID Name phone_no city email
205 mno 90808 bangalore blr@abc.com
100 abc 55555 delhi dlh@abc.com

Please use codetag not icode
# 3  
Old 11-19-2013
Try:
Code:
awk 'NR==FNR{a[$1]=$0; next} $1 in a {print a[$1], $2, $3}' file1 file2

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 11-19-2013
Quote:
Originally Posted by Akshay Hegde
Try :

Code:
$ awk 'FNR==NR{A[$1]=$2 FS $3;next}($1 in A){$0=$0 FS A[$1]}A[$1]' file2 file1
ID Name phone_no city email
205 mno 90808 bangalore blr@abc.com
100 abc 55555 delhi dlh@abc.com

Please use codetag not icode
Thanks AkshaySmilieIt worked perfectlySmilie
One small doubt, what if file1 contains headers and file2 is without headers. In that case will this command work or will there be any other changes in the command

---------- Post updated at 06:37 AM ---------- Previous update was at 06:36 AM ----------

Quote:
Originally Posted by Franklin52
Try:
Code:
awk 'NR==FNR{a[$1]=$0; next} $1 in a {print a[$1], $2, $3}' file1 file2

Thank you Frank Smilie

what if file1 contains headers and file2 is without headers. In that case will this command work or will there be any other changes in the command
# 5  
Old 11-19-2013
Quote:
Originally Posted by bharathbangalor
Thanks AkshaySmilieIt worked perfectlySmilie
One small doubt, what if file1 contains headers and file2 is without headers. In that case will this command work or will there be any other changes in the command

---------- Post updated at 06:37 AM ---------- Previous update was at 06:36 AM ----------
Then Header will not be printed
# 6  
Old 11-19-2013
Sorry for asking too many doubts.

The below code works for fine for files with 3 columns. what if it contains unknown number of columns.In that case how to print all the columns
# 7  
Old 11-19-2013
If you just want to print file1 header you can use this

Code:
$ awk 'FNR==NR{A[$1]=$2 FS $3;next}FNR==1{print}($1 in A){$0=$0 FS A[$1]}A[$1]' file2 file1
ID Name phone_no 
205 mno 90808 bangalore blr@abc.com
100 abc 55555 delhi dlh@abc.com

if you want to print your own header you can do like this
Code:
$ awk 'FNR==NR{A[$1]=$2 FS $3;next}FNR==1{print "My Headers blah blah"}($1 in A){$0=$0 FS A[$1]}A[$1]'  file2  file1
My Headers blah blah
205 mno 90808 bangalore blr@abc.com
100 abc 55555 delhi dlh@abc.com

---------- Post updated at 07:00 AM ---------- Previous update was at 06:54 AM ----------

Quote:
Originally Posted by bharathbangalor
Sorry for asking too many doubts.

The below code works for fine for files with 3 columns. what if it contains unknown number of columns.In that case how to print all the columns
You can select required column

$0 ---> to print entire line
$1 ---> to print 1st column
$2 ---> to print 2nd column
........................................
$n ---> to print nth column
This User Gave Thanks to Akshay Hegde 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

sed Command on Multiple Files and retaining the same file name

Hi All I am trying to run sed command to remove first 2 charcaters from a file on Multiple Files in my directory and what to retain the same file name . I want to know how to retain the same file name but with changes . Can some one please let me know how to proceed with this . ... (7 Replies)
Discussion started by: honey26
7 Replies

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

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

4. Shell Programming and Scripting

Joining two files into one

Hi experts, I'm quite newbie here!! I have two seperate files. Contents of file like below File 1: 6213019212001 8063737 File:2 15703784 I want to join these two files into one where content will be File 3: 6213019212001 8063737 15703784 Regards, Ray Seilden (1 Reply)
Discussion started by: RayanS
1 Replies

5. UNIX for Dummies Questions & Answers

Joining two files

I have two comma separated files. I want to join those filesa nd put the result in separate file. smaple data are: file1: A1,1,100 A2,1,200 B1,2,100 B2,2,200 file2 1,50 1,25 1,25 1,100 1,100 2,50 2,50 (10 Replies)
Discussion started by: pandeesh
10 Replies

6. Shell Programming and Scripting

Merging of files with different headers to make combined headers file

Hi , I have a typical situation. I have 4 files and with different headers (number of headers is varible ). I need to make such a merged file which will have headers combined from all files (comman coluns should appear once only). For example - File 1 H1|H2|H3|H4 11|12|13|14 21|22|23|23... (1 Reply)
Discussion started by: marut_ashu
1 Replies

7. Shell Programming and Scripting

Remove text between headers while leaving headers intact

Hi, I'm trying to strip all lines between two headers in a file: ### BEGIN ### Text to remove, contains all kinds of characters ... Antispyware-Downloadserver.com (Germany)=http://www.antispyware-downloadserver.c om/updates/ Antispyware-Downloadserver.com #2... (3 Replies)
Discussion started by: Trones
3 Replies

8. UNIX for Dummies Questions & Answers

Sort all files in a directory retaining originals

Hi, I need to sort all the files in a directory whilst retaining the originals. So if in the directory I have: File1 File2 File3 I want to sort these files so that I would have: File1 File1.sort File2 File2.sort File3 File3.sort where I have added the .sort extension to show... (4 Replies)
Discussion started by: ledgie
4 Replies

9. UNIX for Dummies Questions & Answers

joining files

Hi, Could anyone help me ? I'm trying to join two files, but no common field are on them. So I think on generate \000\ sequence to add for each line on both files, so then will be able to join these files. Any idea? Thanks in advance, (2 Replies)
Discussion started by: Manu
2 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