Ras
Ras

Reputation: 547

How to diff and patch new lines only

I have a 2 config files(.ini) and I want to get the newly added lines from config file1 to config file2. I don't want to get modified or deleted lines.

To achieve this, I am using below commands.But they are also giving me modified/deleted lines.

diff -u conf2 conf1|grep -E ^\+ >temp
patch -u -o conf2 -i temp

Can you help in getting only newly added lines and to patch them in the exact same line number without using any third party tool. I don't want to patch modified or deleted lines either.

Here're the sample config files.

conf1
# app1 configuration
[app1]
username=root
ssh_port=22
http_port=8080
sd_port=8005

conf2
# app1 configuration
[app1]
username=admin
ssh_port=22
http_port=8080

Now, I have to patch sd_port parameter only. Not the username that got changed.

Thanks.

Upvotes: 1

Views: 1189

Answers (1)

Rico Chen
Rico Chen

Reputation: 2298

This is possible with awk. With

/* diff.awk */

BEGIN {FS="="}
FILENAME==ARGV[1] && $1 !~ /^[#\[]/ {
    a[$1]=$2
} 
FILENAME==ARGV[2] {
    b[$1]=$2
}
END {
    for(i in b) {
        for(j in a) {
            if(!b[j]) {
                c[j]=a[j]
            }
        }
        c[i]=b[i]
    }
    for(k in c) {
        print k"="c[k]
    }
}

Running

awk -f diff.awk conf1 conf2

should give you

sd_port=8005
ssh_port=22
http_port=8080
username=admin

Upvotes: 1

Related Questions