Merging two files in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging two files in UNIX
# 8  
Old 11-22-2013
Code:
awk -F"[\t,]" 'NR==FNR{if (NR==1) {print $0;for (i=1;i<=NF;i++) { d[i]=$i } } else {line[$1]=$0;next }}
{if(FNR==1) {for (j=1;j<=NF;j++) { gsub("\"","",$j);c[j]=$j;}} else {
if ( line[$1] ) {
for ( x=1;x<i;x++ ) {
for ( y=1;y<j;y++ ) {
if (d[x] == c[y] ) { flg=1;printf $y"\t"} else { flag=0}
}
} if ( flag == 0 ) { printf "NA" };printf "\n";
} }}' master slave

# 9  
Old 11-22-2013
Quote:
Originally Posted by pravin27
Code:
awk -F"[\t,]" 'NR==FNR{if (NR==1) {print $0;for (i=1;i<=NF;i++) { d[i]=$i } } else {line[$1]=$0;next }}
{if(FNR==1) {for (j=1;j<=NF;j++) { gsub("\"","",$j);c[j]=$j;}} else {
if ( line[$1] ) {
for ( x=1;x<i;x++ ) {
for ( y=1;y<j;y++ ) {
if (d[x] == c[y] ) { flg=1;printf $y"\t"} else { flag=0}
}
} if ( flag == 0 ) { printf "NA" };printf "\n";
} }}' master slave

Its not working pravin. Smilie The values from slave file are not placed under correct headers. Also values from master file are not displayed.
# 10  
Old 11-22-2013
Quote:
Originally Posted by bharathbangalor
Its not working pravin. Smilie The values from slave file are not placed under correct headers. Also values from master file are not displayed.
If you are trying with data other than which you attached here, our code will not work. we noticed in last 2/3 thread of yours that you are giving sample data which is completely different from your actual data.
# 11  
Old 11-22-2013
Quote:
Originally Posted by Akshay Hegde
If you are trying with data other than which you attached here, our code will not work. we noticed in last 2/3 thread of yours that you are giving sample data which is completely different from your actual data.
Sorry for creating confusion.Smilie
I have attached the actual data,and this is the one which I am actually working on.
# 12  
Old 11-22-2013
You may want to adapt this proposal - which works for your sample data - to your master and slave files:
Code:
awk -F, 'NR>1 {$2=$2 OFS "NA"; print}' OFS="\t" file2 | cat - file1 | sort | tr '\t' ','
1,abc,blr|chn|dlh,500|600|700
1,abc,NA,500|600|700
2,xyz,hyd|blr,888|999
2,xyz,NA,111|222|333
3,mno,blr|hyd,111|222|333
CUST_ID,Name,Place,acc_no

EDIT: sort -n will bring the header to line#1...

Last edited by RudiC; 11-22-2013 at 07:25 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging Files in UNIX shell script

I have the urge to merge some files using unix shell script but I'm very new using this language and I haven't succeeded yet. The requirement is to merge the header, body and footer into one file with the name "ANY-NAME" in below example. To identify which files should be merged, I have flagged... (9 Replies)
Discussion started by: black_soul
9 Replies

2. Shell Programming and Scripting

Merging Very large CSV files in Unix

Hi, I have two very large CSV files, which I want to merge (equi-join) based on a key (column). One of the file (say F1) would have ~30 MM records and 700 columns. The other file (~f2) would have same # of records and lesser columns (say 50). I want to create an output file joining on a... (3 Replies)
Discussion started by: student_007
3 Replies

3. Shell Programming and Scripting

Help with merging 2 files into 1

::::::::: ::FileA:: ::::::::: A1-------A2--------A3---A4---A5-- ================================= AC5VXVLT-XX---------------------- B57E434--XXXX1-----MMMM-ZZZ--111- C325G20--XXXXX3----CCCC------3332 DC35S51--XXXXY1----DDDD------44X- DC35S52--XXXXY2----DDDD------44Y-... (5 Replies)
Discussion started by: lordsmiter
5 Replies

4. UNIX for Dummies Questions & Answers

Merging two files

Hi, I have two files a.txt and b.txt. a.txt 1 2 3 4 b.txt a b c d e I want to generate a file c.txt by merging these two file and the resultant file would contain c.txt 1 (4 Replies)
Discussion started by: siba.s.nayak
4 Replies

5. Shell Programming and Scripting

Help with merging files

i would like to merge two files that have the same format but have different data. i would like to create one output file that contains information from both the original files.:rolleyes: (2 Replies)
Discussion started by: joe black
2 Replies

6. Shell Programming and Scripting

Merging 2 files

Hi, I have got two files 1.txt 1111|apple| 2222|orange| 2.txt 1111|1234|000000000004356| 1111|1234|000000001111| 1111|1234|002000011112| 2222|5678|000000002222| 2222|9102|000000002222| I need to merge these two so that my out put looks like below: Search code being used should be... (4 Replies)
Discussion started by: jisha
4 Replies

7. Shell Programming and Scripting

merging two files

Friends, os: redhat enterprise linux/SCO UNIX5.0 I have two files and I would like to merge on given key value. Now I have tried with join commd but it does not supporte multiple delimiters. and if records length is not fixed. join -a1 5 -a2 1 -t -o file1 file2 > outname Can any... (7 Replies)
Discussion started by: vakharia Mahesh
7 Replies

8. UNIX for Dummies Questions & Answers

Merging files

Hi i have two files say file 1 contents are A B C D E I have file2 contents are B E F G C K I want to have new file like A B (4 Replies)
Discussion started by: ssuresh1999
4 Replies

9. Shell Programming and Scripting

merging files

Thanks in advance I have 2 files having key field in each.I would like to join both on common key.I have used join but not sucessful. The files are attached here . what i Want in the output is on the key field SLS OFFR . I have used join commd but not successful. File one ======= SNO ... (6 Replies)
Discussion started by: vakharia Mahesh
6 Replies

10. UNIX for Dummies Questions & Answers

Merging 2 .CSV files in Unix

I need a little help as I am a complete novice at scripting in unix. However, i am posed with an issue...:eek: i have two csv files in the following format@ FILE1.CSV: HEADER HEADER Header , , HEADER 001X ,,200 002X ,,300 003X ... (6 Replies)
Discussion started by: chachabronson
6 Replies
Login or Register to Ask a Question