To override the contents of files












3















I have two files, file1.txt and file2.txt, file1.txt is:



perimeter a=10
perimeter b=15
perimeter c=20


file2.txt is:



perimeter a=12
perimeter b=14


Let me know the sed or Perl to override the values in file1.txt



Output of file1.txt should be like:



perimeter a=12
perimeter b=14
perimeter c=20


I have used series of command like



sed -i -e '/parameter//d r file1.txt' file2.txt
awk -F, 'NR==FNR{a[$1]=$0;next;}a[$1]{$0=a[$1]}1' file1.txt file2.txt
awk -F, 'NR==FNR{a[$1]=$0;next;}a[$1]{$0=a[$1]}1' file2.txt file1.txt


None of these are helping.










share|improve this question

























  • Is perimeter vs /parameter//d a typo?

    – haukex
    Jan 25 at 13:48











  • Your 2nd awk command is almost right, you're just using the wrong field separator: use = instead of ,

    – glenn jackman
    Jan 25 at 15:34











  • Typo. It has to be permiter.!!

    – Shrinidhi Mohan Deshpande
    Jan 25 at 17:13
















3















I have two files, file1.txt and file2.txt, file1.txt is:



perimeter a=10
perimeter b=15
perimeter c=20


file2.txt is:



perimeter a=12
perimeter b=14


Let me know the sed or Perl to override the values in file1.txt



Output of file1.txt should be like:



perimeter a=12
perimeter b=14
perimeter c=20


I have used series of command like



sed -i -e '/parameter//d r file1.txt' file2.txt
awk -F, 'NR==FNR{a[$1]=$0;next;}a[$1]{$0=a[$1]}1' file1.txt file2.txt
awk -F, 'NR==FNR{a[$1]=$0;next;}a[$1]{$0=a[$1]}1' file2.txt file1.txt


None of these are helping.










share|improve this question

























  • Is perimeter vs /parameter//d a typo?

    – haukex
    Jan 25 at 13:48











  • Your 2nd awk command is almost right, you're just using the wrong field separator: use = instead of ,

    – glenn jackman
    Jan 25 at 15:34











  • Typo. It has to be permiter.!!

    – Shrinidhi Mohan Deshpande
    Jan 25 at 17:13














3












3








3








I have two files, file1.txt and file2.txt, file1.txt is:



perimeter a=10
perimeter b=15
perimeter c=20


file2.txt is:



perimeter a=12
perimeter b=14


Let me know the sed or Perl to override the values in file1.txt



Output of file1.txt should be like:



perimeter a=12
perimeter b=14
perimeter c=20


I have used series of command like



sed -i -e '/parameter//d r file1.txt' file2.txt
awk -F, 'NR==FNR{a[$1]=$0;next;}a[$1]{$0=a[$1]}1' file1.txt file2.txt
awk -F, 'NR==FNR{a[$1]=$0;next;}a[$1]{$0=a[$1]}1' file2.txt file1.txt


None of these are helping.










share|improve this question
















I have two files, file1.txt and file2.txt, file1.txt is:



perimeter a=10
perimeter b=15
perimeter c=20


file2.txt is:



perimeter a=12
perimeter b=14


Let me know the sed or Perl to override the values in file1.txt



Output of file1.txt should be like:



perimeter a=12
perimeter b=14
perimeter c=20


I have used series of command like



sed -i -e '/parameter//d r file1.txt' file2.txt
awk -F, 'NR==FNR{a[$1]=$0;next;}a[$1]{$0=a[$1]}1' file1.txt file2.txt
awk -F, 'NR==FNR{a[$1]=$0;next;}a[$1]{$0=a[$1]}1' file2.txt file1.txt


None of these are helping.







linux command-line sed awk perl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 25 at 13:49









haukex

1176




1176










asked Jan 25 at 13:43









Shrinidhi Mohan DeshpandeShrinidhi Mohan Deshpande

162




162













  • Is perimeter vs /parameter//d a typo?

    – haukex
    Jan 25 at 13:48











  • Your 2nd awk command is almost right, you're just using the wrong field separator: use = instead of ,

    – glenn jackman
    Jan 25 at 15:34











  • Typo. It has to be permiter.!!

    – Shrinidhi Mohan Deshpande
    Jan 25 at 17:13



















  • Is perimeter vs /parameter//d a typo?

    – haukex
    Jan 25 at 13:48











  • Your 2nd awk command is almost right, you're just using the wrong field separator: use = instead of ,

    – glenn jackman
    Jan 25 at 15:34











  • Typo. It has to be permiter.!!

    – Shrinidhi Mohan Deshpande
    Jan 25 at 17:13

















Is perimeter vs /parameter//d a typo?

– haukex
Jan 25 at 13:48





Is perimeter vs /parameter//d a typo?

– haukex
Jan 25 at 13:48













Your 2nd awk command is almost right, you're just using the wrong field separator: use = instead of ,

– glenn jackman
Jan 25 at 15:34





Your 2nd awk command is almost right, you're just using the wrong field separator: use = instead of ,

– glenn jackman
Jan 25 at 15:34













Typo. It has to be permiter.!!

– Shrinidhi Mohan Deshpande
Jan 25 at 17:13





Typo. It has to be permiter.!!

– Shrinidhi Mohan Deshpande
Jan 25 at 17:13










2 Answers
2






active

oldest

votes


















1














Something like this maybe? Save this as e.g. script.pl and run via perl script.pl file1.txt file2.txt and it produces your desired output. I've made some assumptions about the file format, such as that there are no blank lines or comments you need to preserve.



#!/usr/bin/env perl
use warnings;
use strict;
use Tie::IxHash;

tie my %hash, 'Tie::IxHash';
while (<>) {
chomp;
my ($k,$v) = split /=/, $_, 2;
$hash{$k} = $v;
}
for my $k (keys %hash) {
print "$k=$hash{$k}n";
}




Update: A version without the dependency on Tie::IxHash:



use warnings;
use strict;

my (%hash, @keys);
while (<>) {
chomp;
my ($key,$value) = split /=/, $_, 2;
push @keys, $key unless exists $hash{$key};
$hash{$key} = $value;
}
for my $key (@keys) {
print "$key=$hash{$key}n";
}


That can also be smushed into a oneliner:



perl -F= -anle 'exists$h{$F[0]}||push@k,$F[0];$h{$F[0]}=$F[1];END{print"$_=$h{$_}"for@k}' file1.txt file2.txt


However note that unlike the script, this oneliner won't work correctly if there are lines with more than one =.






share|improve this answer





















  • 1





    It is printing perfectly,but when i try to append to file1.txt. i am unable to retain perimeter c =20. I am getting output as perimeter a=12 perimeter b=14 . I have used perl -F= -anle 'exists$h{$F[0]}||push@k,$F[0];$h{$F[0]}=$F[1];END{print"$_=$h{$_}"for@k}' file1.txt file2.txt >file1.txt

    – Shrinidhi Mohan Deshpande
    Jan 25 at 14:44













  • If you're trying to do perl ... file1.txt file2.txt >file1.txt then yes, that won't work. See How can I use a file in a command and redirect output to the same file without truncating it? - for example, try perl ... file1.txt file2.txt | sponge file1.txt, that seems to work for me.

    – haukex
    Jan 25 at 14:51





















0














You'll have to be a bit careful about how to actually rewrite the file. Don't do this:



some_program < file1 > file1


Because the shell takes care of the redirections before launching the program, the > redirection will truncate the file before the program gets a chance to read it.



Here are some alternatives:





  1. explicitly write to a temp file, then overwrite the original



    tmpfile=$(mktemp)
    some_program < file1 > "$tmpfile" && mv "$tmpfile" file1


    Use && so you only mv if the program exited successfully.




  2. use the sponge command from the moreutils package



    some_program < file1 | sponge file1



  3. If you're using GNU awk, you can use the -i inplace extension, but you need to adjust the program a bit:



    gawk -i inplace -F= '
    NR == FNR {a[$1] = $0; print; next}
    $1 in a {$0 = a[$1]}
    1
    ' file{2,1}.txt



Options 2 and 3 use a temp file as well, but will take care of the details for you.






share|improve this answer

























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1398369%2fto-override-the-contents-of-files%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









    1














    Something like this maybe? Save this as e.g. script.pl and run via perl script.pl file1.txt file2.txt and it produces your desired output. I've made some assumptions about the file format, such as that there are no blank lines or comments you need to preserve.



    #!/usr/bin/env perl
    use warnings;
    use strict;
    use Tie::IxHash;

    tie my %hash, 'Tie::IxHash';
    while (<>) {
    chomp;
    my ($k,$v) = split /=/, $_, 2;
    $hash{$k} = $v;
    }
    for my $k (keys %hash) {
    print "$k=$hash{$k}n";
    }




    Update: A version without the dependency on Tie::IxHash:



    use warnings;
    use strict;

    my (%hash, @keys);
    while (<>) {
    chomp;
    my ($key,$value) = split /=/, $_, 2;
    push @keys, $key unless exists $hash{$key};
    $hash{$key} = $value;
    }
    for my $key (@keys) {
    print "$key=$hash{$key}n";
    }


    That can also be smushed into a oneliner:



    perl -F= -anle 'exists$h{$F[0]}||push@k,$F[0];$h{$F[0]}=$F[1];END{print"$_=$h{$_}"for@k}' file1.txt file2.txt


    However note that unlike the script, this oneliner won't work correctly if there are lines with more than one =.






    share|improve this answer





















    • 1





      It is printing perfectly,but when i try to append to file1.txt. i am unable to retain perimeter c =20. I am getting output as perimeter a=12 perimeter b=14 . I have used perl -F= -anle 'exists$h{$F[0]}||push@k,$F[0];$h{$F[0]}=$F[1];END{print"$_=$h{$_}"for@k}' file1.txt file2.txt >file1.txt

      – Shrinidhi Mohan Deshpande
      Jan 25 at 14:44













    • If you're trying to do perl ... file1.txt file2.txt >file1.txt then yes, that won't work. See How can I use a file in a command and redirect output to the same file without truncating it? - for example, try perl ... file1.txt file2.txt | sponge file1.txt, that seems to work for me.

      – haukex
      Jan 25 at 14:51


















    1














    Something like this maybe? Save this as e.g. script.pl and run via perl script.pl file1.txt file2.txt and it produces your desired output. I've made some assumptions about the file format, such as that there are no blank lines or comments you need to preserve.



    #!/usr/bin/env perl
    use warnings;
    use strict;
    use Tie::IxHash;

    tie my %hash, 'Tie::IxHash';
    while (<>) {
    chomp;
    my ($k,$v) = split /=/, $_, 2;
    $hash{$k} = $v;
    }
    for my $k (keys %hash) {
    print "$k=$hash{$k}n";
    }




    Update: A version without the dependency on Tie::IxHash:



    use warnings;
    use strict;

    my (%hash, @keys);
    while (<>) {
    chomp;
    my ($key,$value) = split /=/, $_, 2;
    push @keys, $key unless exists $hash{$key};
    $hash{$key} = $value;
    }
    for my $key (@keys) {
    print "$key=$hash{$key}n";
    }


    That can also be smushed into a oneliner:



    perl -F= -anle 'exists$h{$F[0]}||push@k,$F[0];$h{$F[0]}=$F[1];END{print"$_=$h{$_}"for@k}' file1.txt file2.txt


    However note that unlike the script, this oneliner won't work correctly if there are lines with more than one =.






    share|improve this answer





















    • 1





      It is printing perfectly,but when i try to append to file1.txt. i am unable to retain perimeter c =20. I am getting output as perimeter a=12 perimeter b=14 . I have used perl -F= -anle 'exists$h{$F[0]}||push@k,$F[0];$h{$F[0]}=$F[1];END{print"$_=$h{$_}"for@k}' file1.txt file2.txt >file1.txt

      – Shrinidhi Mohan Deshpande
      Jan 25 at 14:44













    • If you're trying to do perl ... file1.txt file2.txt >file1.txt then yes, that won't work. See How can I use a file in a command and redirect output to the same file without truncating it? - for example, try perl ... file1.txt file2.txt | sponge file1.txt, that seems to work for me.

      – haukex
      Jan 25 at 14:51
















    1












    1








    1







    Something like this maybe? Save this as e.g. script.pl and run via perl script.pl file1.txt file2.txt and it produces your desired output. I've made some assumptions about the file format, such as that there are no blank lines or comments you need to preserve.



    #!/usr/bin/env perl
    use warnings;
    use strict;
    use Tie::IxHash;

    tie my %hash, 'Tie::IxHash';
    while (<>) {
    chomp;
    my ($k,$v) = split /=/, $_, 2;
    $hash{$k} = $v;
    }
    for my $k (keys %hash) {
    print "$k=$hash{$k}n";
    }




    Update: A version without the dependency on Tie::IxHash:



    use warnings;
    use strict;

    my (%hash, @keys);
    while (<>) {
    chomp;
    my ($key,$value) = split /=/, $_, 2;
    push @keys, $key unless exists $hash{$key};
    $hash{$key} = $value;
    }
    for my $key (@keys) {
    print "$key=$hash{$key}n";
    }


    That can also be smushed into a oneliner:



    perl -F= -anle 'exists$h{$F[0]}||push@k,$F[0];$h{$F[0]}=$F[1];END{print"$_=$h{$_}"for@k}' file1.txt file2.txt


    However note that unlike the script, this oneliner won't work correctly if there are lines with more than one =.






    share|improve this answer















    Something like this maybe? Save this as e.g. script.pl and run via perl script.pl file1.txt file2.txt and it produces your desired output. I've made some assumptions about the file format, such as that there are no blank lines or comments you need to preserve.



    #!/usr/bin/env perl
    use warnings;
    use strict;
    use Tie::IxHash;

    tie my %hash, 'Tie::IxHash';
    while (<>) {
    chomp;
    my ($k,$v) = split /=/, $_, 2;
    $hash{$k} = $v;
    }
    for my $k (keys %hash) {
    print "$k=$hash{$k}n";
    }




    Update: A version without the dependency on Tie::IxHash:



    use warnings;
    use strict;

    my (%hash, @keys);
    while (<>) {
    chomp;
    my ($key,$value) = split /=/, $_, 2;
    push @keys, $key unless exists $hash{$key};
    $hash{$key} = $value;
    }
    for my $key (@keys) {
    print "$key=$hash{$key}n";
    }


    That can also be smushed into a oneliner:



    perl -F= -anle 'exists$h{$F[0]}||push@k,$F[0];$h{$F[0]}=$F[1];END{print"$_=$h{$_}"for@k}' file1.txt file2.txt


    However note that unlike the script, this oneliner won't work correctly if there are lines with more than one =.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 25 at 14:25

























    answered Jan 25 at 14:14









    haukexhaukex

    1176




    1176








    • 1





      It is printing perfectly,but when i try to append to file1.txt. i am unable to retain perimeter c =20. I am getting output as perimeter a=12 perimeter b=14 . I have used perl -F= -anle 'exists$h{$F[0]}||push@k,$F[0];$h{$F[0]}=$F[1];END{print"$_=$h{$_}"for@k}' file1.txt file2.txt >file1.txt

      – Shrinidhi Mohan Deshpande
      Jan 25 at 14:44













    • If you're trying to do perl ... file1.txt file2.txt >file1.txt then yes, that won't work. See How can I use a file in a command and redirect output to the same file without truncating it? - for example, try perl ... file1.txt file2.txt | sponge file1.txt, that seems to work for me.

      – haukex
      Jan 25 at 14:51
















    • 1





      It is printing perfectly,but when i try to append to file1.txt. i am unable to retain perimeter c =20. I am getting output as perimeter a=12 perimeter b=14 . I have used perl -F= -anle 'exists$h{$F[0]}||push@k,$F[0];$h{$F[0]}=$F[1];END{print"$_=$h{$_}"for@k}' file1.txt file2.txt >file1.txt

      – Shrinidhi Mohan Deshpande
      Jan 25 at 14:44













    • If you're trying to do perl ... file1.txt file2.txt >file1.txt then yes, that won't work. See How can I use a file in a command and redirect output to the same file without truncating it? - for example, try perl ... file1.txt file2.txt | sponge file1.txt, that seems to work for me.

      – haukex
      Jan 25 at 14:51










    1




    1





    It is printing perfectly,but when i try to append to file1.txt. i am unable to retain perimeter c =20. I am getting output as perimeter a=12 perimeter b=14 . I have used perl -F= -anle 'exists$h{$F[0]}||push@k,$F[0];$h{$F[0]}=$F[1];END{print"$_=$h{$_}"for@k}' file1.txt file2.txt >file1.txt

    – Shrinidhi Mohan Deshpande
    Jan 25 at 14:44







    It is printing perfectly,but when i try to append to file1.txt. i am unable to retain perimeter c =20. I am getting output as perimeter a=12 perimeter b=14 . I have used perl -F= -anle 'exists$h{$F[0]}||push@k,$F[0];$h{$F[0]}=$F[1];END{print"$_=$h{$_}"for@k}' file1.txt file2.txt >file1.txt

    – Shrinidhi Mohan Deshpande
    Jan 25 at 14:44















    If you're trying to do perl ... file1.txt file2.txt >file1.txt then yes, that won't work. See How can I use a file in a command and redirect output to the same file without truncating it? - for example, try perl ... file1.txt file2.txt | sponge file1.txt, that seems to work for me.

    – haukex
    Jan 25 at 14:51







    If you're trying to do perl ... file1.txt file2.txt >file1.txt then yes, that won't work. See How can I use a file in a command and redirect output to the same file without truncating it? - for example, try perl ... file1.txt file2.txt | sponge file1.txt, that seems to work for me.

    – haukex
    Jan 25 at 14:51















    0














    You'll have to be a bit careful about how to actually rewrite the file. Don't do this:



    some_program < file1 > file1


    Because the shell takes care of the redirections before launching the program, the > redirection will truncate the file before the program gets a chance to read it.



    Here are some alternatives:





    1. explicitly write to a temp file, then overwrite the original



      tmpfile=$(mktemp)
      some_program < file1 > "$tmpfile" && mv "$tmpfile" file1


      Use && so you only mv if the program exited successfully.




    2. use the sponge command from the moreutils package



      some_program < file1 | sponge file1



    3. If you're using GNU awk, you can use the -i inplace extension, but you need to adjust the program a bit:



      gawk -i inplace -F= '
      NR == FNR {a[$1] = $0; print; next}
      $1 in a {$0 = a[$1]}
      1
      ' file{2,1}.txt



    Options 2 and 3 use a temp file as well, but will take care of the details for you.






    share|improve this answer






























      0














      You'll have to be a bit careful about how to actually rewrite the file. Don't do this:



      some_program < file1 > file1


      Because the shell takes care of the redirections before launching the program, the > redirection will truncate the file before the program gets a chance to read it.



      Here are some alternatives:





      1. explicitly write to a temp file, then overwrite the original



        tmpfile=$(mktemp)
        some_program < file1 > "$tmpfile" && mv "$tmpfile" file1


        Use && so you only mv if the program exited successfully.




      2. use the sponge command from the moreutils package



        some_program < file1 | sponge file1



      3. If you're using GNU awk, you can use the -i inplace extension, but you need to adjust the program a bit:



        gawk -i inplace -F= '
        NR == FNR {a[$1] = $0; print; next}
        $1 in a {$0 = a[$1]}
        1
        ' file{2,1}.txt



      Options 2 and 3 use a temp file as well, but will take care of the details for you.






      share|improve this answer




























        0












        0








        0







        You'll have to be a bit careful about how to actually rewrite the file. Don't do this:



        some_program < file1 > file1


        Because the shell takes care of the redirections before launching the program, the > redirection will truncate the file before the program gets a chance to read it.



        Here are some alternatives:





        1. explicitly write to a temp file, then overwrite the original



          tmpfile=$(mktemp)
          some_program < file1 > "$tmpfile" && mv "$tmpfile" file1


          Use && so you only mv if the program exited successfully.




        2. use the sponge command from the moreutils package



          some_program < file1 | sponge file1



        3. If you're using GNU awk, you can use the -i inplace extension, but you need to adjust the program a bit:



          gawk -i inplace -F= '
          NR == FNR {a[$1] = $0; print; next}
          $1 in a {$0 = a[$1]}
          1
          ' file{2,1}.txt



        Options 2 and 3 use a temp file as well, but will take care of the details for you.






        share|improve this answer















        You'll have to be a bit careful about how to actually rewrite the file. Don't do this:



        some_program < file1 > file1


        Because the shell takes care of the redirections before launching the program, the > redirection will truncate the file before the program gets a chance to read it.



        Here are some alternatives:





        1. explicitly write to a temp file, then overwrite the original



          tmpfile=$(mktemp)
          some_program < file1 > "$tmpfile" && mv "$tmpfile" file1


          Use && so you only mv if the program exited successfully.




        2. use the sponge command from the moreutils package



          some_program < file1 | sponge file1



        3. If you're using GNU awk, you can use the -i inplace extension, but you need to adjust the program a bit:



          gawk -i inplace -F= '
          NR == FNR {a[$1] = $0; print; next}
          $1 in a {$0 = a[$1]}
          1
          ' file{2,1}.txt



        Options 2 and 3 use a temp file as well, but will take care of the details for you.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 25 at 16:20


























        community wiki





        2 revs
        glenn jackman































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


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




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1398369%2fto-override-the-contents-of-files%23new-answer', 'question_page');
            }
            );

            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







            Popular posts from this blog

            Сан-Квентин

            8-я гвардейская общевойсковая армия

            Алькесар