How to merge two or more fields from two different files where there is non matching column?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to merge two or more fields from two different files where there is non matching column?
# 1  
Old 09-06-2013
How to merge two or more fields from two different files where there is non matching column?

Hi,

Please excuse for often requesting queries and making R&D, I am trying to work out a possibility where i have two files field separated by pipe and another file containing only one field where there is no matching columns, Could you please advise how to merge two files.

Code:
$more top_list.txt_temp_1
PID|USER|PR|NI|VIRT|RES|SHR|S|%CPU|%MEM|TIME+|
2961|aaaa_a|15|0|100m|3196|2624|S|0.0|0.0|0:01.34|
11501|aaaa_a|15|0|100m|3192|2624|S|0.0|0.0|0:00.15|
12759|aaaa_a|16|0|100m|3188|2616|S|0.0|0.0|0:00.49|
21241|aaaa_a|16|0|100m|3200|2628|S|0.0|0.0|0:00.20|
30257|aaaa_a|16|0|100m|3200|2624|S|0.0|0.0|0:00.19|

Code:
$more top_list.txt_temp
COMMAND
ssh aaaa_a@aaaaaa
ssh aaaa_a@aaaaaa
ssh aaaa_a@aaaaaa
ssh aaaa_a@aaaaaa
ssh aaaa_a@aaaaaa

required merged file should be like this.


Code:
PID|USER|PR|NI|VIRT|RES|SHR|S|%CPU|%MEM|TIME+|COMMAND
2961|aaaa_a|15|0|100m|3196|2624|S|0.0|0.0|0:01.34|ssh aaaa_a@aaaaaa
11501|aaaa_a|15|0|100m|3192|2624|S|0.0|0.0|0:00.15|ssh aaaa_a@aaaaaa
12759|aaaa_a|16|0|100m|3188|2616|S|0.0|0.0|0:00.49|ssh aaaa_a@aaaaaa
21241|aaaa_a|16|0|100m|3200|2628|S|0.0|0.0|0:00.20|ssh aaaa_a@aaaaaa
30257|aaaa_a|16|0|100m|3200|2624|S|0.0|0.0|0:00.19|ssh aaaa_a@aaaaaa

I tried using awk command ,

Code:
awk 'FNR == NR { h[$[1]=1; next } !h[$1]'  top_list.txt_temp_1
  top_list.txt_temp

But it is not working and there is no matching column in my two files.

i am not familiar with join and merge using awk.

Please advsie how to merge the two files.

Thanks,
Regards,
karthikram

Last edited by vbe; 09-06-2013 at 10:43 AM..
# 2  
Old 09-06-2013
Hello,

with command paste:
Code:
paste -d'\0' top_list.txt_temp_1 top_list.txt_temp

Regards.
This User Gave Thanks to disedorgue For This Post:
# 3  
Old 09-06-2013
Code:
awk 'FNR==NR{A[NR]=$0;next}{print A[FNR]""$0}' top_list.txt_temp_1 top_list.txt_temp

This User Gave Thanks to pamu For This Post:
# 4  
Old 09-06-2013
Hi disedorgue/pamu,

Thanks both the commands are working good and as expected, very much Thanks for your valueable advise.

Code:
$awk 'FNR==NR{A[NR]=$0;next}{print A[FNR]""$0}' top_list.txt_temp_1 top_list.txt_temp
PID|USER|PR|NI|VIRT|RES|SHR|S|%CPU|%MEM|TIME+|COMMAND
2961|aaaa_a|15|0|100m|3196|2624|S|0.0|0.0|0:01.34|ssh aaaa_a@aaaaaa
11501|aaaa_a|15|0|100m|3192|2624|S|0.0|0.0|0:00.15|ssh aaaa_a@aaaaaa
12759|aaaa_a|16|0|100m|3188|2616|S|0.0|0.0|0:00.49|ssh aaaa_a@aaaaaa
21241|aaaa_a|16|0|100m|3200|2628|S|0.0|0.0|0:00.20|ssh aaaa_a@aaaaaa
30257|aaaa_a|16|0|100m|3200|2624|S|0.0|0.0|0:00.19|ssh aaaa_a@aaaaaa


Code:
$paste -d'\0' top_list.txt_temp_1 top_list.txt_temp
PID|USER|PR|NI|VIRT|RES|SHR|S|%CPU|%MEM|TIME+|COMMAND
2961|aaaa_a|15|0|100m|3196|2624|S|0.0|0.0|0:01.34|ssh aaaa_a@aaaaaa
11501|aaaa_a|15|0|100m|3192|2624|S|0.0|0.0|0:00.15|ssh aaaa_a@aaaaaa
12759|aaaa_a|16|0|100m|3188|2616|S|0.0|0.0|0:00.49|ssh aaaa_a@aaaaaa
21241|aaaa_a|16|0|100m|3200|2628|S|0.0|0.0|0:00.20|ssh aaaa_a@aaaaaa
30257|aaaa_a|16|0|100m|3200|2624|S|0.0|0.0|0:00.19|ssh aaaa_a@aaaaaa



Thanks,
Regards,
karthikram
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing two files by two matching fields

Long time listener first time poster. Hope someone can advise. I have two files, 1000+ lines in each, two fields in each file. After performing a sort, what is the best way to find exact matches where field $1 and $2 in file1 are also present in file2 on the same line, then output only those... (6 Replies)
Discussion started by: bstaff
6 Replies

2. UNIX for Beginners Questions & Answers

Awk: matching multiple fields between 2 files

Hi, I have 2 tab-delimited input files as follows. file1.tab: green A apple red B apple file2.tab: apple - A;Z Objective: Return $1 of file1 if, . $1 of file2 matches $3 of file1 and, . any single element (separated by ";") in $3 of file2 is present in $2 of file1 In order to... (3 Replies)
Discussion started by: beca123456
3 Replies

3. Linux

Merge two files based on matching criteria

Hi, I am trying to merge two csv files based on matching criteria: File description is as below : Key_File : 000|ÇÞ|Key_HF|ÇÞ|Key_FName 001|ÇÞ|Key_11|ÇÞ|Sort_Key22|ÇÞ|Key_31 002|ÇÞ|Key_12|ÇÞ|Sort_Key23|ÇÞ|Key_32 003|ÇÞ|Key_13|ÇÞ|Sort_Key24|ÇÞ|Key_33 050|ÇÞ|Key_15|ÇÞ|Sort_Key25|ÇÞ|Key_34... (3 Replies)
Discussion started by: PK29
3 Replies

4. UNIX for Dummies Questions & Answers

Merge two text files by two fields and mixed output

Hello, I'm back again looking for your precious help- This time I need to merge two text files with matching two fields, output only common records with mixed output. Let's look at the example: FILE1 56153;AAA0708;3;TEST1TEST1; 89014;BBB0708;3;TEST2TEST2; 89014;BBB0708;4;TEST3TEST3; ... (7 Replies)
Discussion started by: emare
7 Replies

5. Shell Programming and Scripting

How to merge two files with unique values matching.?

I have one script as below: #!/bin/ksh Outputfile1="/home/OutputFile1.xls" Outputfile2="/home/OutputFile2.xls" InputFile1="/home/InputFile1.sql" InputFile2="/home/InputFile2.sql" echo "Select hobby, class, subject, sports, rollNumber from Student_Table" >> InputFile1 echo "Select rollNumber... (3 Replies)
Discussion started by: Sharma331
3 Replies

6. Shell Programming and Scripting

Need to merge multiple text files vertically and place comma between fields

Hello expert friends, I'm writing a script to capture stats using sar and stuck up at report generation. I have around 10 files in a directory and need to merge them all vertically based on the time value of first column (output file should have only one time value) and insert comma after... (6 Replies)
Discussion started by: prvnrk
6 Replies

7. Shell Programming and Scripting

Merge two files matching columns

Hi! I need to merge two files when col1 (x:x:x) matching and adds second column from file1.txt. # cat 1.txt aaa;a12 bbb;b13 ccc;c33 ddd;d55 eee;e11 # cat 2.txt bbb;b55;34444;d55 aaa;a15;35666;a44 I try with this awk and I get succesfully first column from 1.txt: # awk -F";"... (2 Replies)
Discussion started by: fhluque
2 Replies

8. Shell Programming and Scripting

Matching multiple fields from two files and then some?

Hi, I am working with two tab-delimited files with multiple columns, formatted as follows: File 1: >chrom 1 100 A G 20 …(10 columns) >chrom 1 104 G C 18 …(10 columns) >chrom 2 28 T C ... (4 Replies)
Discussion started by: mbp
4 Replies

9. Shell Programming and Scripting

comparing two files for matching fields

I am newbie to unix and would please like some help to solve the task below I have two files, file_a.text and file_b.text that I want to evaluate. file_a.text 1698.74 1711.88 6576.25 899.41 3205.63 4187.98 697.35 1551.83 ... (3 Replies)
Discussion started by: gameli
3 Replies

10. Shell Programming and Scripting

AWK: merge two files and replace some fields

Need some code tweak: awk 'END { for (i=1; i<=n; i++) if (f2]) print f2] } NR == FNR { f2 = $1] = $0 next } $1 in f2 { delete f2 }1' FS=, OFS=, 2.csv 1.csv > 3.csvfile 1.csv have: $1,$2,$3,$4,$5,$6,$7,$8,$9...... file 2.csv have: $1,$2,$3,$4,$5,$6 (2 Replies)
Discussion started by: u10
2 Replies
Login or Register to Ask a Question