Diff 2 settings files and replace the difference
I have 2 files with settings:
file1.txt and file2.txt
A=1 A=2
B=3 B=3
C=5 C=4
D=6 .
. E=7
I am looking for the best approach to replace the values of the file1.txt with the diff values of file2.txt, so the file1.txt would look like:
file1.txt:
A=2
B=3
C=4
D=6
E=7
Currently i didn't write any code, but the only approach i think about is to write a bash script that diffs both files (provided as positional arguments), and use sed to replace non-matching strings. Something in this vein:
./diffreplace.bash file1.txt file2.txt > NEWfile1.txt
I wonder whether there is something more elegant that alerady exists?
linux bash
add a comment |
I have 2 files with settings:
file1.txt and file2.txt
A=1 A=2
B=3 B=3
C=5 C=4
D=6 .
. E=7
I am looking for the best approach to replace the values of the file1.txt with the diff values of file2.txt, so the file1.txt would look like:
file1.txt:
A=2
B=3
C=4
D=6
E=7
Currently i didn't write any code, but the only approach i think about is to write a bash script that diffs both files (provided as positional arguments), and use sed to replace non-matching strings. Something in this vein:
./diffreplace.bash file1.txt file2.txt > NEWfile1.txt
I wonder whether there is something more elegant that alerady exists?
linux bash
1
Are theA=
s etc. always on the same lines? It that really a period.
for empty value?
– James Brown
Nov 13 '18 at 13:11
no no, the lines are not constant. no period for non-existing value, it was jut for demonstration
– faceless
Nov 13 '18 at 13:23
Why don't you simply overwrite the file?
– Dominique
Nov 13 '18 at 13:41
@Dominique - the contents of both files may differ, overwriting would harm different versions these files should serve.
– faceless
Nov 13 '18 at 13:44
@faceless See stackoverflow.com/help/mcve and edit your question accordingly.
– oguzismail
Nov 13 '18 at 14:47
add a comment |
I have 2 files with settings:
file1.txt and file2.txt
A=1 A=2
B=3 B=3
C=5 C=4
D=6 .
. E=7
I am looking for the best approach to replace the values of the file1.txt with the diff values of file2.txt, so the file1.txt would look like:
file1.txt:
A=2
B=3
C=4
D=6
E=7
Currently i didn't write any code, but the only approach i think about is to write a bash script that diffs both files (provided as positional arguments), and use sed to replace non-matching strings. Something in this vein:
./diffreplace.bash file1.txt file2.txt > NEWfile1.txt
I wonder whether there is something more elegant that alerady exists?
linux bash
I have 2 files with settings:
file1.txt and file2.txt
A=1 A=2
B=3 B=3
C=5 C=4
D=6 .
. E=7
I am looking for the best approach to replace the values of the file1.txt with the diff values of file2.txt, so the file1.txt would look like:
file1.txt:
A=2
B=3
C=4
D=6
E=7
Currently i didn't write any code, but the only approach i think about is to write a bash script that diffs both files (provided as positional arguments), and use sed to replace non-matching strings. Something in this vein:
./diffreplace.bash file1.txt file2.txt > NEWfile1.txt
I wonder whether there is something more elegant that alerady exists?
linux bash
linux bash
asked Nov 13 '18 at 12:57
facelessfaceless
1337
1337
1
Are theA=
s etc. always on the same lines? It that really a period.
for empty value?
– James Brown
Nov 13 '18 at 13:11
no no, the lines are not constant. no period for non-existing value, it was jut for demonstration
– faceless
Nov 13 '18 at 13:23
Why don't you simply overwrite the file?
– Dominique
Nov 13 '18 at 13:41
@Dominique - the contents of both files may differ, overwriting would harm different versions these files should serve.
– faceless
Nov 13 '18 at 13:44
@faceless See stackoverflow.com/help/mcve and edit your question accordingly.
– oguzismail
Nov 13 '18 at 14:47
add a comment |
1
Are theA=
s etc. always on the same lines? It that really a period.
for empty value?
– James Brown
Nov 13 '18 at 13:11
no no, the lines are not constant. no period for non-existing value, it was jut for demonstration
– faceless
Nov 13 '18 at 13:23
Why don't you simply overwrite the file?
– Dominique
Nov 13 '18 at 13:41
@Dominique - the contents of both files may differ, overwriting would harm different versions these files should serve.
– faceless
Nov 13 '18 at 13:44
@faceless See stackoverflow.com/help/mcve and edit your question accordingly.
– oguzismail
Nov 13 '18 at 14:47
1
1
Are the
A=
s etc. always on the same lines? It that really a period .
for empty value?– James Brown
Nov 13 '18 at 13:11
Are the
A=
s etc. always on the same lines? It that really a period .
for empty value?– James Brown
Nov 13 '18 at 13:11
no no, the lines are not constant. no period for non-existing value, it was jut for demonstration
– faceless
Nov 13 '18 at 13:23
no no, the lines are not constant. no period for non-existing value, it was jut for demonstration
– faceless
Nov 13 '18 at 13:23
Why don't you simply overwrite the file?
– Dominique
Nov 13 '18 at 13:41
Why don't you simply overwrite the file?
– Dominique
Nov 13 '18 at 13:41
@Dominique - the contents of both files may differ, overwriting would harm different versions these files should serve.
– faceless
Nov 13 '18 at 13:44
@Dominique - the contents of both files may differ, overwriting would harm different versions these files should serve.
– faceless
Nov 13 '18 at 13:44
@faceless See stackoverflow.com/help/mcve and edit your question accordingly.
– oguzismail
Nov 13 '18 at 14:47
@faceless See stackoverflow.com/help/mcve and edit your question accordingly.
– oguzismail
Nov 13 '18 at 14:47
add a comment |
2 Answers
2
active
oldest
votes
All of the following solutions may change the order of assignments. I assumed that would be ok.
Lazy Solution
If you use these assignments in some way that allows overwriting, then you can simple append file2
to the end of file1
. All old values will be overwritten be the new ones when you execute result
.
cat old new > result
Slightly Better Solution
Extending the previous approach, you can iterate over the lines of result
and for every variable, keep only the last assignment:
cat new old |
awk -F= '{if (a[$1]!="x") {print $0; a[$1]=x}}'
Alternative Solution
Use join
to combine both files, then filter out the values from the first file by using cut
. When your files are sorted, use
join -t= -a1 -a2 new old | cut -d= -f1,2
if not, use
join -t= -a1 -a2 <(sort new) <(sort old) |
cut -d= -f1,2
the structure of the file must remain untouched. Sort mixes the order.
– faceless
Nov 13 '18 at 13:34
And what structure is that? A, if it exists, is always on the first line in both files or empty, B on the second etc.?
– James Brown
Nov 13 '18 at 13:41
@Socowi - the second 'join' solution worked the best for my need. But there is one thing - I need to merge the diffs into the 'new' file. When using the statement as you suggested I can see the right output on the screen. But if I add to it '> new' - the 'new' file gets relpaced by the 'old', and contains no lines that doesn't appear in the 'old'. Despite there are few and I could see it before redirecting the output. Do you have any idea why?
– faceless
Nov 15 '18 at 18:41
1
@faceless You cannot redirect to a file while reading it. See this question. Redirect to a temporary file or usesponge
frommoreutils
.
– Socowi
Nov 15 '18 at 19:47
@Socowi - thanks for your last comment, it helped. Another query for 'join' solution. When i run it on a big (prod) files, i get "join: /file/:number: is not sorted: pattern=...". Thus i get duplicate entries in the output for that pattern, because there is no '-f2' for 'cut' command. I tried several articles that explain how to fix sorting issue (-d, --nocheck-order,...) but noone has remediated the issue. These unsorted patterns are on different lines in each file, so maybe this is the reason? Please help to resolve it as i'd really like to keep using the simplisity of join in my solution.
– faceless
Nov 18 '18 at 10:32
add a comment |
I'm a little puzzed over your comment the structure of the file must remain untouched. Sort mixes the order so I'm assuming that the As are always on line 1 or line 1 is .
etc:
$ awk '
BEGIN { RS="r?n" } # in case of Windows line-endings
$0!="." { # we dont store . (change it to null if you need to)
a[FNR]=$0 # hash using line number as key
}
END { # after all that hashing
for(i=1;i<=FNR;i++) # iterate in line number order
print a[i] # output the last met version
}' file1 file2 # mind the file order
Output:
A=2
B=3
C=4
D=6
E=7
Edit: A version with a whitelist:
$ cat whitelist
A
B
E
Script:
$ awk -F= '
NR==FNR { # process the whitelist
a[FNR]=$1 # for a key is linenumber, record as value
b[$1]=FNR # bor b record is key, linenumber is value
n=FNR # remember the count for END
next
} # process file1 and file2 ... filen
($1 in b) { # if record is found in b
a[b[$1]]=$0 # we set the record to a[linenumber]=record
}
END {
for(i=1;i<=n;i++) # here we loop on linenumbers, 1 to n
print a[i]
}' whitelist file1 file2
Output:
A=2
B=3
E=7
I need to test it and will approve a bit later. The concern is that the lines in both files are not constant and may vary, whereas the variables' names always the same. Also is this possible in this approach to define a "white list" for the settings of the first file that must not be changed?
– faceless
Nov 13 '18 at 14:13
Answering my previous comment I assume some sort must be applied to make the task easier
– faceless
Nov 13 '18 at 14:14
white list - sure.
– James Brown
Nov 13 '18 at 14:53
i wonder why you and me getting the different results for the same code and the same files. When i test your first solution (w/o whitelist) i receive in the output the only contents of the 'file2' - not appended to differences of 'file1', but overidden. I get only A=2 B=3 C=4 E=7 But no "D" is inn the output. I am not deeply familiar with AWK, but could you please explain this difference as this approach seems to be very elegant and i'd like to lear more about it on example.
– faceless
Nov 14 '18 at 13:13
as for now i have created the following fork to count the differences between the files (with whitelist). But it uses a different approach. More character consuming: freetexthost.com/i30r14dghs
– faceless
Nov 14 '18 at 13:16
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281528%2fdiff-2-settings-files-and-replace-the-difference%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
All of the following solutions may change the order of assignments. I assumed that would be ok.
Lazy Solution
If you use these assignments in some way that allows overwriting, then you can simple append file2
to the end of file1
. All old values will be overwritten be the new ones when you execute result
.
cat old new > result
Slightly Better Solution
Extending the previous approach, you can iterate over the lines of result
and for every variable, keep only the last assignment:
cat new old |
awk -F= '{if (a[$1]!="x") {print $0; a[$1]=x}}'
Alternative Solution
Use join
to combine both files, then filter out the values from the first file by using cut
. When your files are sorted, use
join -t= -a1 -a2 new old | cut -d= -f1,2
if not, use
join -t= -a1 -a2 <(sort new) <(sort old) |
cut -d= -f1,2
the structure of the file must remain untouched. Sort mixes the order.
– faceless
Nov 13 '18 at 13:34
And what structure is that? A, if it exists, is always on the first line in both files or empty, B on the second etc.?
– James Brown
Nov 13 '18 at 13:41
@Socowi - the second 'join' solution worked the best for my need. But there is one thing - I need to merge the diffs into the 'new' file. When using the statement as you suggested I can see the right output on the screen. But if I add to it '> new' - the 'new' file gets relpaced by the 'old', and contains no lines that doesn't appear in the 'old'. Despite there are few and I could see it before redirecting the output. Do you have any idea why?
– faceless
Nov 15 '18 at 18:41
1
@faceless You cannot redirect to a file while reading it. See this question. Redirect to a temporary file or usesponge
frommoreutils
.
– Socowi
Nov 15 '18 at 19:47
@Socowi - thanks for your last comment, it helped. Another query for 'join' solution. When i run it on a big (prod) files, i get "join: /file/:number: is not sorted: pattern=...". Thus i get duplicate entries in the output for that pattern, because there is no '-f2' for 'cut' command. I tried several articles that explain how to fix sorting issue (-d, --nocheck-order,...) but noone has remediated the issue. These unsorted patterns are on different lines in each file, so maybe this is the reason? Please help to resolve it as i'd really like to keep using the simplisity of join in my solution.
– faceless
Nov 18 '18 at 10:32
add a comment |
All of the following solutions may change the order of assignments. I assumed that would be ok.
Lazy Solution
If you use these assignments in some way that allows overwriting, then you can simple append file2
to the end of file1
. All old values will be overwritten be the new ones when you execute result
.
cat old new > result
Slightly Better Solution
Extending the previous approach, you can iterate over the lines of result
and for every variable, keep only the last assignment:
cat new old |
awk -F= '{if (a[$1]!="x") {print $0; a[$1]=x}}'
Alternative Solution
Use join
to combine both files, then filter out the values from the first file by using cut
. When your files are sorted, use
join -t= -a1 -a2 new old | cut -d= -f1,2
if not, use
join -t= -a1 -a2 <(sort new) <(sort old) |
cut -d= -f1,2
the structure of the file must remain untouched. Sort mixes the order.
– faceless
Nov 13 '18 at 13:34
And what structure is that? A, if it exists, is always on the first line in both files or empty, B on the second etc.?
– James Brown
Nov 13 '18 at 13:41
@Socowi - the second 'join' solution worked the best for my need. But there is one thing - I need to merge the diffs into the 'new' file. When using the statement as you suggested I can see the right output on the screen. But if I add to it '> new' - the 'new' file gets relpaced by the 'old', and contains no lines that doesn't appear in the 'old'. Despite there are few and I could see it before redirecting the output. Do you have any idea why?
– faceless
Nov 15 '18 at 18:41
1
@faceless You cannot redirect to a file while reading it. See this question. Redirect to a temporary file or usesponge
frommoreutils
.
– Socowi
Nov 15 '18 at 19:47
@Socowi - thanks for your last comment, it helped. Another query for 'join' solution. When i run it on a big (prod) files, i get "join: /file/:number: is not sorted: pattern=...". Thus i get duplicate entries in the output for that pattern, because there is no '-f2' for 'cut' command. I tried several articles that explain how to fix sorting issue (-d, --nocheck-order,...) but noone has remediated the issue. These unsorted patterns are on different lines in each file, so maybe this is the reason? Please help to resolve it as i'd really like to keep using the simplisity of join in my solution.
– faceless
Nov 18 '18 at 10:32
add a comment |
All of the following solutions may change the order of assignments. I assumed that would be ok.
Lazy Solution
If you use these assignments in some way that allows overwriting, then you can simple append file2
to the end of file1
. All old values will be overwritten be the new ones when you execute result
.
cat old new > result
Slightly Better Solution
Extending the previous approach, you can iterate over the lines of result
and for every variable, keep only the last assignment:
cat new old |
awk -F= '{if (a[$1]!="x") {print $0; a[$1]=x}}'
Alternative Solution
Use join
to combine both files, then filter out the values from the first file by using cut
. When your files are sorted, use
join -t= -a1 -a2 new old | cut -d= -f1,2
if not, use
join -t= -a1 -a2 <(sort new) <(sort old) |
cut -d= -f1,2
All of the following solutions may change the order of assignments. I assumed that would be ok.
Lazy Solution
If you use these assignments in some way that allows overwriting, then you can simple append file2
to the end of file1
. All old values will be overwritten be the new ones when you execute result
.
cat old new > result
Slightly Better Solution
Extending the previous approach, you can iterate over the lines of result
and for every variable, keep only the last assignment:
cat new old |
awk -F= '{if (a[$1]!="x") {print $0; a[$1]=x}}'
Alternative Solution
Use join
to combine both files, then filter out the values from the first file by using cut
. When your files are sorted, use
join -t= -a1 -a2 new old | cut -d= -f1,2
if not, use
join -t= -a1 -a2 <(sort new) <(sort old) |
cut -d= -f1,2
edited Nov 13 '18 at 15:45
answered Nov 13 '18 at 13:15
SocowiSocowi
6,2102724
6,2102724
the structure of the file must remain untouched. Sort mixes the order.
– faceless
Nov 13 '18 at 13:34
And what structure is that? A, if it exists, is always on the first line in both files or empty, B on the second etc.?
– James Brown
Nov 13 '18 at 13:41
@Socowi - the second 'join' solution worked the best for my need. But there is one thing - I need to merge the diffs into the 'new' file. When using the statement as you suggested I can see the right output on the screen. But if I add to it '> new' - the 'new' file gets relpaced by the 'old', and contains no lines that doesn't appear in the 'old'. Despite there are few and I could see it before redirecting the output. Do you have any idea why?
– faceless
Nov 15 '18 at 18:41
1
@faceless You cannot redirect to a file while reading it. See this question. Redirect to a temporary file or usesponge
frommoreutils
.
– Socowi
Nov 15 '18 at 19:47
@Socowi - thanks for your last comment, it helped. Another query for 'join' solution. When i run it on a big (prod) files, i get "join: /file/:number: is not sorted: pattern=...". Thus i get duplicate entries in the output for that pattern, because there is no '-f2' for 'cut' command. I tried several articles that explain how to fix sorting issue (-d, --nocheck-order,...) but noone has remediated the issue. These unsorted patterns are on different lines in each file, so maybe this is the reason? Please help to resolve it as i'd really like to keep using the simplisity of join in my solution.
– faceless
Nov 18 '18 at 10:32
add a comment |
the structure of the file must remain untouched. Sort mixes the order.
– faceless
Nov 13 '18 at 13:34
And what structure is that? A, if it exists, is always on the first line in both files or empty, B on the second etc.?
– James Brown
Nov 13 '18 at 13:41
@Socowi - the second 'join' solution worked the best for my need. But there is one thing - I need to merge the diffs into the 'new' file. When using the statement as you suggested I can see the right output on the screen. But if I add to it '> new' - the 'new' file gets relpaced by the 'old', and contains no lines that doesn't appear in the 'old'. Despite there are few and I could see it before redirecting the output. Do you have any idea why?
– faceless
Nov 15 '18 at 18:41
1
@faceless You cannot redirect to a file while reading it. See this question. Redirect to a temporary file or usesponge
frommoreutils
.
– Socowi
Nov 15 '18 at 19:47
@Socowi - thanks for your last comment, it helped. Another query for 'join' solution. When i run it on a big (prod) files, i get "join: /file/:number: is not sorted: pattern=...". Thus i get duplicate entries in the output for that pattern, because there is no '-f2' for 'cut' command. I tried several articles that explain how to fix sorting issue (-d, --nocheck-order,...) but noone has remediated the issue. These unsorted patterns are on different lines in each file, so maybe this is the reason? Please help to resolve it as i'd really like to keep using the simplisity of join in my solution.
– faceless
Nov 18 '18 at 10:32
the structure of the file must remain untouched. Sort mixes the order.
– faceless
Nov 13 '18 at 13:34
the structure of the file must remain untouched. Sort mixes the order.
– faceless
Nov 13 '18 at 13:34
And what structure is that? A, if it exists, is always on the first line in both files or empty, B on the second etc.?
– James Brown
Nov 13 '18 at 13:41
And what structure is that? A, if it exists, is always on the first line in both files or empty, B on the second etc.?
– James Brown
Nov 13 '18 at 13:41
@Socowi - the second 'join' solution worked the best for my need. But there is one thing - I need to merge the diffs into the 'new' file. When using the statement as you suggested I can see the right output on the screen. But if I add to it '> new' - the 'new' file gets relpaced by the 'old', and contains no lines that doesn't appear in the 'old'. Despite there are few and I could see it before redirecting the output. Do you have any idea why?
– faceless
Nov 15 '18 at 18:41
@Socowi - the second 'join' solution worked the best for my need. But there is one thing - I need to merge the diffs into the 'new' file. When using the statement as you suggested I can see the right output on the screen. But if I add to it '> new' - the 'new' file gets relpaced by the 'old', and contains no lines that doesn't appear in the 'old'. Despite there are few and I could see it before redirecting the output. Do you have any idea why?
– faceless
Nov 15 '18 at 18:41
1
1
@faceless You cannot redirect to a file while reading it. See this question. Redirect to a temporary file or use
sponge
from moreutils
.– Socowi
Nov 15 '18 at 19:47
@faceless You cannot redirect to a file while reading it. See this question. Redirect to a temporary file or use
sponge
from moreutils
.– Socowi
Nov 15 '18 at 19:47
@Socowi - thanks for your last comment, it helped. Another query for 'join' solution. When i run it on a big (prod) files, i get "join: /file/:number: is not sorted: pattern=...". Thus i get duplicate entries in the output for that pattern, because there is no '-f2' for 'cut' command. I tried several articles that explain how to fix sorting issue (-d, --nocheck-order,...) but noone has remediated the issue. These unsorted patterns are on different lines in each file, so maybe this is the reason? Please help to resolve it as i'd really like to keep using the simplisity of join in my solution.
– faceless
Nov 18 '18 at 10:32
@Socowi - thanks for your last comment, it helped. Another query for 'join' solution. When i run it on a big (prod) files, i get "join: /file/:number: is not sorted: pattern=...". Thus i get duplicate entries in the output for that pattern, because there is no '-f2' for 'cut' command. I tried several articles that explain how to fix sorting issue (-d, --nocheck-order,...) but noone has remediated the issue. These unsorted patterns are on different lines in each file, so maybe this is the reason? Please help to resolve it as i'd really like to keep using the simplisity of join in my solution.
– faceless
Nov 18 '18 at 10:32
add a comment |
I'm a little puzzed over your comment the structure of the file must remain untouched. Sort mixes the order so I'm assuming that the As are always on line 1 or line 1 is .
etc:
$ awk '
BEGIN { RS="r?n" } # in case of Windows line-endings
$0!="." { # we dont store . (change it to null if you need to)
a[FNR]=$0 # hash using line number as key
}
END { # after all that hashing
for(i=1;i<=FNR;i++) # iterate in line number order
print a[i] # output the last met version
}' file1 file2 # mind the file order
Output:
A=2
B=3
C=4
D=6
E=7
Edit: A version with a whitelist:
$ cat whitelist
A
B
E
Script:
$ awk -F= '
NR==FNR { # process the whitelist
a[FNR]=$1 # for a key is linenumber, record as value
b[$1]=FNR # bor b record is key, linenumber is value
n=FNR # remember the count for END
next
} # process file1 and file2 ... filen
($1 in b) { # if record is found in b
a[b[$1]]=$0 # we set the record to a[linenumber]=record
}
END {
for(i=1;i<=n;i++) # here we loop on linenumbers, 1 to n
print a[i]
}' whitelist file1 file2
Output:
A=2
B=3
E=7
I need to test it and will approve a bit later. The concern is that the lines in both files are not constant and may vary, whereas the variables' names always the same. Also is this possible in this approach to define a "white list" for the settings of the first file that must not be changed?
– faceless
Nov 13 '18 at 14:13
Answering my previous comment I assume some sort must be applied to make the task easier
– faceless
Nov 13 '18 at 14:14
white list - sure.
– James Brown
Nov 13 '18 at 14:53
i wonder why you and me getting the different results for the same code and the same files. When i test your first solution (w/o whitelist) i receive in the output the only contents of the 'file2' - not appended to differences of 'file1', but overidden. I get only A=2 B=3 C=4 E=7 But no "D" is inn the output. I am not deeply familiar with AWK, but could you please explain this difference as this approach seems to be very elegant and i'd like to lear more about it on example.
– faceless
Nov 14 '18 at 13:13
as for now i have created the following fork to count the differences between the files (with whitelist). But it uses a different approach. More character consuming: freetexthost.com/i30r14dghs
– faceless
Nov 14 '18 at 13:16
|
show 3 more comments
I'm a little puzzed over your comment the structure of the file must remain untouched. Sort mixes the order so I'm assuming that the As are always on line 1 or line 1 is .
etc:
$ awk '
BEGIN { RS="r?n" } # in case of Windows line-endings
$0!="." { # we dont store . (change it to null if you need to)
a[FNR]=$0 # hash using line number as key
}
END { # after all that hashing
for(i=1;i<=FNR;i++) # iterate in line number order
print a[i] # output the last met version
}' file1 file2 # mind the file order
Output:
A=2
B=3
C=4
D=6
E=7
Edit: A version with a whitelist:
$ cat whitelist
A
B
E
Script:
$ awk -F= '
NR==FNR { # process the whitelist
a[FNR]=$1 # for a key is linenumber, record as value
b[$1]=FNR # bor b record is key, linenumber is value
n=FNR # remember the count for END
next
} # process file1 and file2 ... filen
($1 in b) { # if record is found in b
a[b[$1]]=$0 # we set the record to a[linenumber]=record
}
END {
for(i=1;i<=n;i++) # here we loop on linenumbers, 1 to n
print a[i]
}' whitelist file1 file2
Output:
A=2
B=3
E=7
I need to test it and will approve a bit later. The concern is that the lines in both files are not constant and may vary, whereas the variables' names always the same. Also is this possible in this approach to define a "white list" for the settings of the first file that must not be changed?
– faceless
Nov 13 '18 at 14:13
Answering my previous comment I assume some sort must be applied to make the task easier
– faceless
Nov 13 '18 at 14:14
white list - sure.
– James Brown
Nov 13 '18 at 14:53
i wonder why you and me getting the different results for the same code and the same files. When i test your first solution (w/o whitelist) i receive in the output the only contents of the 'file2' - not appended to differences of 'file1', but overidden. I get only A=2 B=3 C=4 E=7 But no "D" is inn the output. I am not deeply familiar with AWK, but could you please explain this difference as this approach seems to be very elegant and i'd like to lear more about it on example.
– faceless
Nov 14 '18 at 13:13
as for now i have created the following fork to count the differences between the files (with whitelist). But it uses a different approach. More character consuming: freetexthost.com/i30r14dghs
– faceless
Nov 14 '18 at 13:16
|
show 3 more comments
I'm a little puzzed over your comment the structure of the file must remain untouched. Sort mixes the order so I'm assuming that the As are always on line 1 or line 1 is .
etc:
$ awk '
BEGIN { RS="r?n" } # in case of Windows line-endings
$0!="." { # we dont store . (change it to null if you need to)
a[FNR]=$0 # hash using line number as key
}
END { # after all that hashing
for(i=1;i<=FNR;i++) # iterate in line number order
print a[i] # output the last met version
}' file1 file2 # mind the file order
Output:
A=2
B=3
C=4
D=6
E=7
Edit: A version with a whitelist:
$ cat whitelist
A
B
E
Script:
$ awk -F= '
NR==FNR { # process the whitelist
a[FNR]=$1 # for a key is linenumber, record as value
b[$1]=FNR # bor b record is key, linenumber is value
n=FNR # remember the count for END
next
} # process file1 and file2 ... filen
($1 in b) { # if record is found in b
a[b[$1]]=$0 # we set the record to a[linenumber]=record
}
END {
for(i=1;i<=n;i++) # here we loop on linenumbers, 1 to n
print a[i]
}' whitelist file1 file2
Output:
A=2
B=3
E=7
I'm a little puzzed over your comment the structure of the file must remain untouched. Sort mixes the order so I'm assuming that the As are always on line 1 or line 1 is .
etc:
$ awk '
BEGIN { RS="r?n" } # in case of Windows line-endings
$0!="." { # we dont store . (change it to null if you need to)
a[FNR]=$0 # hash using line number as key
}
END { # after all that hashing
for(i=1;i<=FNR;i++) # iterate in line number order
print a[i] # output the last met version
}' file1 file2 # mind the file order
Output:
A=2
B=3
C=4
D=6
E=7
Edit: A version with a whitelist:
$ cat whitelist
A
B
E
Script:
$ awk -F= '
NR==FNR { # process the whitelist
a[FNR]=$1 # for a key is linenumber, record as value
b[$1]=FNR # bor b record is key, linenumber is value
n=FNR # remember the count for END
next
} # process file1 and file2 ... filen
($1 in b) { # if record is found in b
a[b[$1]]=$0 # we set the record to a[linenumber]=record
}
END {
for(i=1;i<=n;i++) # here we loop on linenumbers, 1 to n
print a[i]
}' whitelist file1 file2
Output:
A=2
B=3
E=7
edited Nov 14 '18 at 13:55
answered Nov 13 '18 at 13:47
James BrownJames Brown
18.3k31635
18.3k31635
I need to test it and will approve a bit later. The concern is that the lines in both files are not constant and may vary, whereas the variables' names always the same. Also is this possible in this approach to define a "white list" for the settings of the first file that must not be changed?
– faceless
Nov 13 '18 at 14:13
Answering my previous comment I assume some sort must be applied to make the task easier
– faceless
Nov 13 '18 at 14:14
white list - sure.
– James Brown
Nov 13 '18 at 14:53
i wonder why you and me getting the different results for the same code and the same files. When i test your first solution (w/o whitelist) i receive in the output the only contents of the 'file2' - not appended to differences of 'file1', but overidden. I get only A=2 B=3 C=4 E=7 But no "D" is inn the output. I am not deeply familiar with AWK, but could you please explain this difference as this approach seems to be very elegant and i'd like to lear more about it on example.
– faceless
Nov 14 '18 at 13:13
as for now i have created the following fork to count the differences between the files (with whitelist). But it uses a different approach. More character consuming: freetexthost.com/i30r14dghs
– faceless
Nov 14 '18 at 13:16
|
show 3 more comments
I need to test it and will approve a bit later. The concern is that the lines in both files are not constant and may vary, whereas the variables' names always the same. Also is this possible in this approach to define a "white list" for the settings of the first file that must not be changed?
– faceless
Nov 13 '18 at 14:13
Answering my previous comment I assume some sort must be applied to make the task easier
– faceless
Nov 13 '18 at 14:14
white list - sure.
– James Brown
Nov 13 '18 at 14:53
i wonder why you and me getting the different results for the same code and the same files. When i test your first solution (w/o whitelist) i receive in the output the only contents of the 'file2' - not appended to differences of 'file1', but overidden. I get only A=2 B=3 C=4 E=7 But no "D" is inn the output. I am not deeply familiar with AWK, but could you please explain this difference as this approach seems to be very elegant and i'd like to lear more about it on example.
– faceless
Nov 14 '18 at 13:13
as for now i have created the following fork to count the differences between the files (with whitelist). But it uses a different approach. More character consuming: freetexthost.com/i30r14dghs
– faceless
Nov 14 '18 at 13:16
I need to test it and will approve a bit later. The concern is that the lines in both files are not constant and may vary, whereas the variables' names always the same. Also is this possible in this approach to define a "white list" for the settings of the first file that must not be changed?
– faceless
Nov 13 '18 at 14:13
I need to test it and will approve a bit later. The concern is that the lines in both files are not constant and may vary, whereas the variables' names always the same. Also is this possible in this approach to define a "white list" for the settings of the first file that must not be changed?
– faceless
Nov 13 '18 at 14:13
Answering my previous comment I assume some sort must be applied to make the task easier
– faceless
Nov 13 '18 at 14:14
Answering my previous comment I assume some sort must be applied to make the task easier
– faceless
Nov 13 '18 at 14:14
white list - sure.
– James Brown
Nov 13 '18 at 14:53
white list - sure.
– James Brown
Nov 13 '18 at 14:53
i wonder why you and me getting the different results for the same code and the same files. When i test your first solution (w/o whitelist) i receive in the output the only contents of the 'file2' - not appended to differences of 'file1', but overidden. I get only A=2 B=3 C=4 E=7 But no "D" is inn the output. I am not deeply familiar with AWK, but could you please explain this difference as this approach seems to be very elegant and i'd like to lear more about it on example.
– faceless
Nov 14 '18 at 13:13
i wonder why you and me getting the different results for the same code and the same files. When i test your first solution (w/o whitelist) i receive in the output the only contents of the 'file2' - not appended to differences of 'file1', but overidden. I get only A=2 B=3 C=4 E=7 But no "D" is inn the output. I am not deeply familiar with AWK, but could you please explain this difference as this approach seems to be very elegant and i'd like to lear more about it on example.
– faceless
Nov 14 '18 at 13:13
as for now i have created the following fork to count the differences between the files (with whitelist). But it uses a different approach. More character consuming: freetexthost.com/i30r14dghs
– faceless
Nov 14 '18 at 13:16
as for now i have created the following fork to count the differences between the files (with whitelist). But it uses a different approach. More character consuming: freetexthost.com/i30r14dghs
– faceless
Nov 14 '18 at 13:16
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281528%2fdiff-2-settings-files-and-replace-the-difference%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Are the
A=
s etc. always on the same lines? It that really a period.
for empty value?– James Brown
Nov 13 '18 at 13:11
no no, the lines are not constant. no period for non-existing value, it was jut for demonstration
– faceless
Nov 13 '18 at 13:23
Why don't you simply overwrite the file?
– Dominique
Nov 13 '18 at 13:41
@Dominique - the contents of both files may differ, overwriting would harm different versions these files should serve.
– faceless
Nov 13 '18 at 13:44
@faceless See stackoverflow.com/help/mcve and edit your question accordingly.
– oguzismail
Nov 13 '18 at 14:47