Read from and append to file at the same time while preserving end-of-line

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















Conceptually I have an easy task... I have [loosely] structured data in a file:



Testing: debug, default CXXFLAGS
<100's of additional output lines>
Testing: release, default CXXFLAGS
<100's of additional output lines>
...


I try to summarize it in a log file:



echo "Configurations tested:" | tee -a "$TEST_RESULTS"
echo $($GREP 'Testing: ' "$TEST_RESULTS" | $SED 's/Testing: / * /g') | tee -a "$TEST_RESULTS"


Instead of:



Configurations tested:
* debug, default CXXFLAGS
* release, default CXXFLAGS


I get:



Configurations tested:
1 3way.cpp 3way.h CMakeLists.txt CMakeLists.txt.diff Doxyfile Filelist.txt GNUmakefile
GNUmakefile-cross Install.txt License.txt Readme.txt TestData TestVectors adhoc.cpp.proto
adler32.cpp adler32.h aes.h algebra.cpp algebra.h ...


I think I am wreaking havoc on the file buffer $TEST_RESULTS because its being read from in the grep, and written to with the tee.



When I attempt to put the result of $GREP 'Testing: ' "$TEST_RESULTS" | $SED 's/Testing: / * /g' in a shell variable, I loose the line endings which results in one big concatenation:



* debug, default CXXFLAGS * release, default CXXFLAGS ... <30 additional configs>


How do I read from and append to a file at the same time while preserving the end-of-lines?




I've made some progress with:



ESCAPED=$($GREP 'Testing: ' "$TEST_RESULTS" | $AWK -F ": " 'print " -" $2 "$"')
echo $ESCAPED | tr $ 'n' | tee -a "$TEST_RESULTS"


However, it can't use * as a bullet point, and it seems to drop leading space:



Configurations tested:
-debug, default CXXFLAGS
-release, default CXXFLAGS


I'm not using sed because swapping-in a new line is an absolute pain across platforms. Platforms include BSD, Cygwin, Linux, OS X, Solaris.










share|improve this question



















  • 1





    The problem with * and spaces is that you left out the double quotes around variable substitutions.

    – Gilles
    Jun 18 '16 at 20:44

















2















Conceptually I have an easy task... I have [loosely] structured data in a file:



Testing: debug, default CXXFLAGS
<100's of additional output lines>
Testing: release, default CXXFLAGS
<100's of additional output lines>
...


I try to summarize it in a log file:



echo "Configurations tested:" | tee -a "$TEST_RESULTS"
echo $($GREP 'Testing: ' "$TEST_RESULTS" | $SED 's/Testing: / * /g') | tee -a "$TEST_RESULTS"


Instead of:



Configurations tested:
* debug, default CXXFLAGS
* release, default CXXFLAGS


I get:



Configurations tested:
1 3way.cpp 3way.h CMakeLists.txt CMakeLists.txt.diff Doxyfile Filelist.txt GNUmakefile
GNUmakefile-cross Install.txt License.txt Readme.txt TestData TestVectors adhoc.cpp.proto
adler32.cpp adler32.h aes.h algebra.cpp algebra.h ...


I think I am wreaking havoc on the file buffer $TEST_RESULTS because its being read from in the grep, and written to with the tee.



When I attempt to put the result of $GREP 'Testing: ' "$TEST_RESULTS" | $SED 's/Testing: / * /g' in a shell variable, I loose the line endings which results in one big concatenation:



* debug, default CXXFLAGS * release, default CXXFLAGS ... <30 additional configs>


How do I read from and append to a file at the same time while preserving the end-of-lines?




I've made some progress with:



ESCAPED=$($GREP 'Testing: ' "$TEST_RESULTS" | $AWK -F ": " 'print " -" $2 "$"')
echo $ESCAPED | tr $ 'n' | tee -a "$TEST_RESULTS"


However, it can't use * as a bullet point, and it seems to drop leading space:



Configurations tested:
-debug, default CXXFLAGS
-release, default CXXFLAGS


I'm not using sed because swapping-in a new line is an absolute pain across platforms. Platforms include BSD, Cygwin, Linux, OS X, Solaris.










share|improve this question



















  • 1





    The problem with * and spaces is that you left out the double quotes around variable substitutions.

    – Gilles
    Jun 18 '16 at 20:44













2












2








2








Conceptually I have an easy task... I have [loosely] structured data in a file:



Testing: debug, default CXXFLAGS
<100's of additional output lines>
Testing: release, default CXXFLAGS
<100's of additional output lines>
...


I try to summarize it in a log file:



echo "Configurations tested:" | tee -a "$TEST_RESULTS"
echo $($GREP 'Testing: ' "$TEST_RESULTS" | $SED 's/Testing: / * /g') | tee -a "$TEST_RESULTS"


Instead of:



Configurations tested:
* debug, default CXXFLAGS
* release, default CXXFLAGS


I get:



Configurations tested:
1 3way.cpp 3way.h CMakeLists.txt CMakeLists.txt.diff Doxyfile Filelist.txt GNUmakefile
GNUmakefile-cross Install.txt License.txt Readme.txt TestData TestVectors adhoc.cpp.proto
adler32.cpp adler32.h aes.h algebra.cpp algebra.h ...


I think I am wreaking havoc on the file buffer $TEST_RESULTS because its being read from in the grep, and written to with the tee.



When I attempt to put the result of $GREP 'Testing: ' "$TEST_RESULTS" | $SED 's/Testing: / * /g' in a shell variable, I loose the line endings which results in one big concatenation:



* debug, default CXXFLAGS * release, default CXXFLAGS ... <30 additional configs>


How do I read from and append to a file at the same time while preserving the end-of-lines?




I've made some progress with:



ESCAPED=$($GREP 'Testing: ' "$TEST_RESULTS" | $AWK -F ": " 'print " -" $2 "$"')
echo $ESCAPED | tr $ 'n' | tee -a "$TEST_RESULTS"


However, it can't use * as a bullet point, and it seems to drop leading space:



Configurations tested:
-debug, default CXXFLAGS
-release, default CXXFLAGS


I'm not using sed because swapping-in a new line is an absolute pain across platforms. Platforms include BSD, Cygwin, Linux, OS X, Solaris.










share|improve this question
















Conceptually I have an easy task... I have [loosely] structured data in a file:



Testing: debug, default CXXFLAGS
<100's of additional output lines>
Testing: release, default CXXFLAGS
<100's of additional output lines>
...


I try to summarize it in a log file:



echo "Configurations tested:" | tee -a "$TEST_RESULTS"
echo $($GREP 'Testing: ' "$TEST_RESULTS" | $SED 's/Testing: / * /g') | tee -a "$TEST_RESULTS"


Instead of:



Configurations tested:
* debug, default CXXFLAGS
* release, default CXXFLAGS


I get:



Configurations tested:
1 3way.cpp 3way.h CMakeLists.txt CMakeLists.txt.diff Doxyfile Filelist.txt GNUmakefile
GNUmakefile-cross Install.txt License.txt Readme.txt TestData TestVectors adhoc.cpp.proto
adler32.cpp adler32.h aes.h algebra.cpp algebra.h ...


I think I am wreaking havoc on the file buffer $TEST_RESULTS because its being read from in the grep, and written to with the tee.



When I attempt to put the result of $GREP 'Testing: ' "$TEST_RESULTS" | $SED 's/Testing: / * /g' in a shell variable, I loose the line endings which results in one big concatenation:



* debug, default CXXFLAGS * release, default CXXFLAGS ... <30 additional configs>


How do I read from and append to a file at the same time while preserving the end-of-lines?




I've made some progress with:



ESCAPED=$($GREP 'Testing: ' "$TEST_RESULTS" | $AWK -F ": " 'print " -" $2 "$"')
echo $ESCAPED | tr $ 'n' | tee -a "$TEST_RESULTS"


However, it can't use * as a bullet point, and it seems to drop leading space:



Configurations tested:
-debug, default CXXFLAGS
-release, default CXXFLAGS


I'm not using sed because swapping-in a new line is an absolute pain across platforms. Platforms include BSD, Cygwin, Linux, OS X, Solaris.







bash text-processing awk sed grep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 18 '16 at 13:47







jww

















asked Jun 18 '16 at 12:20









jwwjww

1,61932668




1,61932668







  • 1





    The problem with * and spaces is that you left out the double quotes around variable substitutions.

    – Gilles
    Jun 18 '16 at 20:44












  • 1





    The problem with * and spaces is that you left out the double quotes around variable substitutions.

    – Gilles
    Jun 18 '16 at 20:44







1




1





The problem with * and spaces is that you left out the double quotes around variable substitutions.

– Gilles
Jun 18 '16 at 20:44





The problem with * and spaces is that you left out the double quotes around variable substitutions.

– Gilles
Jun 18 '16 at 20:44










2 Answers
2






active

oldest

votes


















0














I would try something like:



awk -v pattern="Testing:" '$0 ~ pattern sub(pattern, " *"); print ' 


This should work with any version of sed, since it contains no obvious extensions. It is not necessary to handle the newlines explicitly as long as you take care to quote your variables properly, in order to prevent word splitting.






share|improve this answer

























  • "Frankly, I don't see why you are messing around with the newlines...." - I told you why: "When I attempt to put the result of <command above> in a shell variable, I loose the line endings which results in one big concatenation".

    – jww
    Jun 29 '16 at 1:06












  • Well, if you don't want the shell to do its thing, then use quotes. echo $var is not the same as echo "$var".

    – Michael Vehrs
    Jun 29 '16 at 6:00











  • Thanks. " if you don't want the shell to do its thing, then use quotes" - OK, could you provide it in your answer? Also, does your Awk work everywhere? I try to avoid Sed because of the different behavior on BSDs, Linux, OS X, Solaris and Unix. Its been my experience that Awk can be a pain point too.

    – jww
    Jun 29 '16 at 6:07


















0














Assuming $TEST_RESULTS is the name of a file that contains the test output. If I understand correctly, you want to append the list of tested configurations to that same file.



So, use a temporary file. Then append that file to the original.



tmpfile="$( mktemp )"
echo "Configurations tested:" >"$tmpfile"
sed -n 's/^Testing: (.*)/ * 1/p' "$TEST_RESULTS" >>"$tmpfile"

cat "$tmpfile" >>"$TEST_RESULTS"
rm -f "$tmpfile"


If you don't have mktemp, you could use



tmpfile="$TEST_RESULTS.tmp"


or



tmpfile="/tmp/$( basename "$TEST_RESULTS" ).tmp"


or something similar... assuming that file doesn't already exist (in which case it will be overwritten and then removed).






share|improve this answer























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    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: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%2funix.stackexchange.com%2fquestions%2f290578%2fread-from-and-append-to-file-at-the-same-time-while-preserving-end-of-line%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









    0














    I would try something like:



    awk -v pattern="Testing:" '$0 ~ pattern sub(pattern, " *"); print ' 


    This should work with any version of sed, since it contains no obvious extensions. It is not necessary to handle the newlines explicitly as long as you take care to quote your variables properly, in order to prevent word splitting.






    share|improve this answer

























    • "Frankly, I don't see why you are messing around with the newlines...." - I told you why: "When I attempt to put the result of <command above> in a shell variable, I loose the line endings which results in one big concatenation".

      – jww
      Jun 29 '16 at 1:06












    • Well, if you don't want the shell to do its thing, then use quotes. echo $var is not the same as echo "$var".

      – Michael Vehrs
      Jun 29 '16 at 6:00











    • Thanks. " if you don't want the shell to do its thing, then use quotes" - OK, could you provide it in your answer? Also, does your Awk work everywhere? I try to avoid Sed because of the different behavior on BSDs, Linux, OS X, Solaris and Unix. Its been my experience that Awk can be a pain point too.

      – jww
      Jun 29 '16 at 6:07















    0














    I would try something like:



    awk -v pattern="Testing:" '$0 ~ pattern sub(pattern, " *"); print ' 


    This should work with any version of sed, since it contains no obvious extensions. It is not necessary to handle the newlines explicitly as long as you take care to quote your variables properly, in order to prevent word splitting.






    share|improve this answer

























    • "Frankly, I don't see why you are messing around with the newlines...." - I told you why: "When I attempt to put the result of <command above> in a shell variable, I loose the line endings which results in one big concatenation".

      – jww
      Jun 29 '16 at 1:06












    • Well, if you don't want the shell to do its thing, then use quotes. echo $var is not the same as echo "$var".

      – Michael Vehrs
      Jun 29 '16 at 6:00











    • Thanks. " if you don't want the shell to do its thing, then use quotes" - OK, could you provide it in your answer? Also, does your Awk work everywhere? I try to avoid Sed because of the different behavior on BSDs, Linux, OS X, Solaris and Unix. Its been my experience that Awk can be a pain point too.

      – jww
      Jun 29 '16 at 6:07













    0












    0








    0







    I would try something like:



    awk -v pattern="Testing:" '$0 ~ pattern sub(pattern, " *"); print ' 


    This should work with any version of sed, since it contains no obvious extensions. It is not necessary to handle the newlines explicitly as long as you take care to quote your variables properly, in order to prevent word splitting.






    share|improve this answer















    I would try something like:



    awk -v pattern="Testing:" '$0 ~ pattern sub(pattern, " *"); print ' 


    This should work with any version of sed, since it contains no obvious extensions. It is not necessary to handle the newlines explicitly as long as you take care to quote your variables properly, in order to prevent word splitting.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jun 29 '16 at 7:12

























    answered Jun 20 '16 at 8:46









    Michael VehrsMichael Vehrs

    2,18037




    2,18037












    • "Frankly, I don't see why you are messing around with the newlines...." - I told you why: "When I attempt to put the result of <command above> in a shell variable, I loose the line endings which results in one big concatenation".

      – jww
      Jun 29 '16 at 1:06












    • Well, if you don't want the shell to do its thing, then use quotes. echo $var is not the same as echo "$var".

      – Michael Vehrs
      Jun 29 '16 at 6:00











    • Thanks. " if you don't want the shell to do its thing, then use quotes" - OK, could you provide it in your answer? Also, does your Awk work everywhere? I try to avoid Sed because of the different behavior on BSDs, Linux, OS X, Solaris and Unix. Its been my experience that Awk can be a pain point too.

      – jww
      Jun 29 '16 at 6:07

















    • "Frankly, I don't see why you are messing around with the newlines...." - I told you why: "When I attempt to put the result of <command above> in a shell variable, I loose the line endings which results in one big concatenation".

      – jww
      Jun 29 '16 at 1:06












    • Well, if you don't want the shell to do its thing, then use quotes. echo $var is not the same as echo "$var".

      – Michael Vehrs
      Jun 29 '16 at 6:00











    • Thanks. " if you don't want the shell to do its thing, then use quotes" - OK, could you provide it in your answer? Also, does your Awk work everywhere? I try to avoid Sed because of the different behavior on BSDs, Linux, OS X, Solaris and Unix. Its been my experience that Awk can be a pain point too.

      – jww
      Jun 29 '16 at 6:07
















    "Frankly, I don't see why you are messing around with the newlines...." - I told you why: "When I attempt to put the result of <command above> in a shell variable, I loose the line endings which results in one big concatenation".

    – jww
    Jun 29 '16 at 1:06






    "Frankly, I don't see why you are messing around with the newlines...." - I told you why: "When I attempt to put the result of <command above> in a shell variable, I loose the line endings which results in one big concatenation".

    – jww
    Jun 29 '16 at 1:06














    Well, if you don't want the shell to do its thing, then use quotes. echo $var is not the same as echo "$var".

    – Michael Vehrs
    Jun 29 '16 at 6:00





    Well, if you don't want the shell to do its thing, then use quotes. echo $var is not the same as echo "$var".

    – Michael Vehrs
    Jun 29 '16 at 6:00













    Thanks. " if you don't want the shell to do its thing, then use quotes" - OK, could you provide it in your answer? Also, does your Awk work everywhere? I try to avoid Sed because of the different behavior on BSDs, Linux, OS X, Solaris and Unix. Its been my experience that Awk can be a pain point too.

    – jww
    Jun 29 '16 at 6:07





    Thanks. " if you don't want the shell to do its thing, then use quotes" - OK, could you provide it in your answer? Also, does your Awk work everywhere? I try to avoid Sed because of the different behavior on BSDs, Linux, OS X, Solaris and Unix. Its been my experience that Awk can be a pain point too.

    – jww
    Jun 29 '16 at 6:07













    0














    Assuming $TEST_RESULTS is the name of a file that contains the test output. If I understand correctly, you want to append the list of tested configurations to that same file.



    So, use a temporary file. Then append that file to the original.



    tmpfile="$( mktemp )"
    echo "Configurations tested:" >"$tmpfile"
    sed -n 's/^Testing: (.*)/ * 1/p' "$TEST_RESULTS" >>"$tmpfile"

    cat "$tmpfile" >>"$TEST_RESULTS"
    rm -f "$tmpfile"


    If you don't have mktemp, you could use



    tmpfile="$TEST_RESULTS.tmp"


    or



    tmpfile="/tmp/$( basename "$TEST_RESULTS" ).tmp"


    or something similar... assuming that file doesn't already exist (in which case it will be overwritten and then removed).






    share|improve this answer



























      0














      Assuming $TEST_RESULTS is the name of a file that contains the test output. If I understand correctly, you want to append the list of tested configurations to that same file.



      So, use a temporary file. Then append that file to the original.



      tmpfile="$( mktemp )"
      echo "Configurations tested:" >"$tmpfile"
      sed -n 's/^Testing: (.*)/ * 1/p' "$TEST_RESULTS" >>"$tmpfile"

      cat "$tmpfile" >>"$TEST_RESULTS"
      rm -f "$tmpfile"


      If you don't have mktemp, you could use



      tmpfile="$TEST_RESULTS.tmp"


      or



      tmpfile="/tmp/$( basename "$TEST_RESULTS" ).tmp"


      or something similar... assuming that file doesn't already exist (in which case it will be overwritten and then removed).






      share|improve this answer

























        0












        0








        0







        Assuming $TEST_RESULTS is the name of a file that contains the test output. If I understand correctly, you want to append the list of tested configurations to that same file.



        So, use a temporary file. Then append that file to the original.



        tmpfile="$( mktemp )"
        echo "Configurations tested:" >"$tmpfile"
        sed -n 's/^Testing: (.*)/ * 1/p' "$TEST_RESULTS" >>"$tmpfile"

        cat "$tmpfile" >>"$TEST_RESULTS"
        rm -f "$tmpfile"


        If you don't have mktemp, you could use



        tmpfile="$TEST_RESULTS.tmp"


        or



        tmpfile="/tmp/$( basename "$TEST_RESULTS" ).tmp"


        or something similar... assuming that file doesn't already exist (in which case it will be overwritten and then removed).






        share|improve this answer













        Assuming $TEST_RESULTS is the name of a file that contains the test output. If I understand correctly, you want to append the list of tested configurations to that same file.



        So, use a temporary file. Then append that file to the original.



        tmpfile="$( mktemp )"
        echo "Configurations tested:" >"$tmpfile"
        sed -n 's/^Testing: (.*)/ * 1/p' "$TEST_RESULTS" >>"$tmpfile"

        cat "$tmpfile" >>"$TEST_RESULTS"
        rm -f "$tmpfile"


        If you don't have mktemp, you could use



        tmpfile="$TEST_RESULTS.tmp"


        or



        tmpfile="/tmp/$( basename "$TEST_RESULTS" ).tmp"


        or something similar... assuming that file doesn't already exist (in which case it will be overwritten and then removed).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 5 '17 at 11:40









        KusalanandaKusalananda

        140k17261435




        140k17261435



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • 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%2funix.stackexchange.com%2fquestions%2f290578%2fread-from-and-append-to-file-at-the-same-time-while-preserving-end-of-line%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

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)