Merge two files by condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge two files by condition
# 1  
Old 10-13-2012
Merge two files by condition

I have two files as below
A file
Code:
/* comment for id1 */
"id1" = "x1"

/* comment for id2 */
"id2" = "x2"

/* comment for id3 */
"id3" = "x3"

B file
Code:
/* comment for id1 */
"id1" = "y1"

/* comment for id2 */
"id2" = "x2"

I hope output file as below
Code:
/* comment for id1 */
"id1" = "y1"

/* comment for id2 */
"id2" = "x2"

/* comment for id3 */
"id3" = "x3"

Condition looks like
1. All comment lines in A file will output
2. if same comment line in B file, line below that comment line in B file will output
3. if no same comment line in B file, line below comment line in A file will output

Does anyone help me?
# 2  
Old 10-13-2012
Code:
awk 'NR==FNR && /^\/\*/{getline m;a[$0]=m; next;} /^\/\*/{getline n;b[$0]=n;}
 END{ for (i in a){
 if( i in b){ print i"\n"b[i]; }else
 print i"\n"a[i];
 }}' file1 file2

# 3  
Old 10-13-2012
Hi mikezang,

One way:
Code:
$ awk '
    FNR == NR && $0 ~ /^\/\*/ { 
        getline codeB; 
        comments[ $0 ] = codeB; 
        next; 
    } 

    $0 ~ /^\/\*/ { 
        if ( $0 in comments ) { 
            codeA = comments[ $0 ]; 
        } 
        else { 
            getline codeA; 
        } 
        printf "%s\n%s\n", $0, codeA;
        next;
    }

    FNR < NR && /^[[:space:]]*$/
' fileB fileA
/* comment for id1 */
"id1" = "y1"

/* comment for id2 */
"id2" = "x2"

/* comment for id3 */
"id3" = "x3"

# 4  
Old 10-13-2012
A bit shorter:
Code:
awk 'NR==FNR&&/^\/\*/{x=$0;getline;a[x]=$0;next}/^\/\*/{x=$0;print;getline;$0=a[x]?a[x]:$0;printf $0"\n\n"}' B A

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 10-13-2012
Code:
awk 'FNR==NR{if($0 ~ /^\//){a[$4]=$0}else{a[$1]=$3};next}{
if($0 ~ /^\//){print a[$4]?a[$4]:$0}else{if(a[$1] != $3){print $1,$2,a[$1]?a[$1]:$3}else{print $0}}}' file2 file1

This User Gave Thanks to pamu For This Post:
# 6  
Old 10-13-2012
Code:
awk 'NR==FNR{if (!/^\/\*/) A[$1]=$3; next} $1 in A && !/^\/\*/{$3=A[$1]}1' B A

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 10-13-2012
Quote:
Originally Posted by bartus11
A bit shorter:
Code:
awk 'NR==FNR&&/^\/\*/{x=$0;getline;a[x]=$0;next}/^\/\*/{x=$0;print;getline;$0=a[x]?a[x]:$0;printf $0"\n\n"}' B A

Thanks for your shorter answer!
If I want to output to file B, what can I do?

---------- Post updated at 04:08 ---------- Previous update was at 04:07 ----------

Quote:
Originally Posted by Scrutinizer
Code:
awk 'NR==FNR{if (!/^\/\*/) A[$1]=$3; next} $1 in A && !/^\/\*/{$3=A[$1]}1' B A

Thanks for your shortest answer, How can I output to B file?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

5. Shell Programming and Scripting

How to compare 2 files & get only few columns based on a condition related to both files?

Hiiiii friends I have 2 files which contains huge data & few lines of it are as shown below File1: b.dat(which has 21 columns) SSR 1976 8 12 13 10 44.00 39.0700 70.7800 7.0 0 0.00 0 2.78 0.00 0.00 0 0.00 2.78 0 NULL ISC 1976 8 12 22 32 37.39 36.2942 70.7338... (6 Replies)
Discussion started by: reva
6 Replies

6. Shell Programming and Scripting

Merge 2 files

Hello, i'd like a bash script to merge 2 files without duplicate lines. Example : file1 : toto titi file2 : toto tata Expected result, file3 : toto (5 Replies)
Discussion started by: Celmar
5 Replies

7. Shell Programming and Scripting

merge files

hi i have two files file1 1234567 1234678 1234679 file2 98765|jjkskk|9393|iyutr 98765|kkooo|9393|hjjjd 98765|abcvfg|9393|sskds output should be 1234567|jjkskk|9393|iyutr 1234678|kkooo|9393|hjjjd 1234679|abcvfg|9393|sskds (5 Replies)
Discussion started by: mad_man12
5 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

how to merge these two files?

I have two files, each of them has 12 lines, fileA has 3 columns, fileB has 1 column, like the following FileA a 1 b 2 c 3 ..blabla FileB A B C ..blabla Now I am trying to put the content of fileB as column 3 of fileA, e.g. a 1 A b 2 B c 3 C (3 Replies)
Discussion started by: fedora
3 Replies

10. 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
Login or Register to Ask a Question