Append herestring data to a file, if it doesn't already exist in that file (all in one line)

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











up vote
-3
down vote

favorite
1












What will be an "elegant" one-line way to append a single line of data into the end of a file, with herestring, if this exact data isn't already in that file?



This is my herestring append pattern:



cat >> /var/www/html/myFile <<< "myOutput"






share|improve this question






















  • See also Edit file based on existence of a string
    – don_crissti
    Jan 18 at 0:06














up vote
-3
down vote

favorite
1












What will be an "elegant" one-line way to append a single line of data into the end of a file, with herestring, if this exact data isn't already in that file?



This is my herestring append pattern:



cat >> /var/www/html/myFile <<< "myOutput"






share|improve this question






















  • See also Edit file based on existence of a string
    – don_crissti
    Jan 18 at 0:06












up vote
-3
down vote

favorite
1









up vote
-3
down vote

favorite
1






1





What will be an "elegant" one-line way to append a single line of data into the end of a file, with herestring, if this exact data isn't already in that file?



This is my herestring append pattern:



cat >> /var/www/html/myFile <<< "myOutput"






share|improve this question














What will be an "elegant" one-line way to append a single line of data into the end of a file, with herestring, if this exact data isn't already in that file?



This is my herestring append pattern:



cat >> /var/www/html/myFile <<< "myOutput"








share|improve this question













share|improve this question




share|improve this question








edited Jan 15 at 9:01

























asked Jan 14 at 21:35









Arcticooling

83123




83123











  • See also Edit file based on existence of a string
    – don_crissti
    Jan 18 at 0:06
















  • See also Edit file based on existence of a string
    – don_crissti
    Jan 18 at 0:06















See also Edit file based on existence of a string
– don_crissti
Jan 18 at 0:06




See also Edit file based on existence of a string
– don_crissti
Jan 18 at 0:06










3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










Assuming that you want "myOutput" to be a full line
at the last line of the file:



s="myOutput"; sed '$ /^'"$s"'$/b; s/$/n'"$s"'/' file


s="myOutput" Set pattern to match
sed use sed
$ …' file On the last line of the file
/^'"$s"'$/b Match the pattern as a whole line and branch to end if matched.
s/$/n'"$s"'/ If no match, append the pattern at the end line.



If the file already contains the "myOutput" data
somewhere other than the last line,
this will add another copy of it at the end of the file.






share|improve this answer






















  • (1) Your answer doesn’t actually append the new line of text to the file (as the question requests and demonstrates) — you would need to use sed -i.  (2) Your command doesn’t work when the text string contains a slash.
    – Scott
    Jan 17 at 23:48

















up vote
3
down vote













s="myOutput"; grep -Fxqe "$s" < "$file" || printf "%sn" "$s" >> "$file"





share|improve this answer



























    up vote
    -1
    down vote













    In below script will check in complete last line for whether variable i present or not if not it inserts variable i in last line of file.



    As tested it worked fine



    #!bin/bash
    i="praveen"
    f=$(sed -n '$p' p.txt | sed -n "/^$i$/p")
    if [[ -z $f ]]
    then
    sed "$s/.*/&n$i/g" p.txt
    else
    echo "output exsists"
    fi





    share|improve this answer




















    • (1) The question says, “all in one line”.  You’ll notice that the earlier answers are one line long.  Yours is nine lines long (counting everything).  (2) You tested this?  It doesn’t work AT ALL because the she-bang is wrong: there needs to be a / at the beginning of the interpreter name (after the #!).  (3) s/$/nfoo/ (which isaac’s answer uses) is a somewhat awkward way to add a line of text (containing “foo”) to a file in sed. (As I’m sure you understand, … (Cont’d)
      – Scott
      Jan 17 at 23:44










    • (Cont’d) …  a $ at the beginning of that command (before the s) represents the last line of the file, and says “do this at the end of the file”.)  s/.*/&nfoo/ (which your answer uses) is an unnecessarily complicated version of the same command.  But isaac’s answer uses that awkward technique because he’s trying to solve the problem economically (i.e., in one line) and he’s using sed already. Your use of it in a standalone command (the sixth line of your script) is doubly unnecessarily complicated and unintuitive, because, in that context, you don’t need to be using sed. … (Cont’d)
      – Scott
      Jan 17 at 23:44










    • (Cont’d) …  It would be much more natural to do what the OP is already doing (in the question): cat >> "$file" <<< "$i" — or, better yet, something like Stéphane’s version: printf "%sn" "$i" >> "$file". (4) The question asks how to append a line of text to a file. Your answer doesn’t even accomplish that (although, to be fair, isaac’s doesn’t either). To append a line of text to a file using your command, you would need to use sed -i. (5) Your command doesn’t work when the text string contains a slash.
      – Scott
      Jan 17 at 23:44










    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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f417117%2fappend-herestring-data-to-a-file-if-it-doesnt-already-exist-in-that-file-all%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Assuming that you want "myOutput" to be a full line
    at the last line of the file:



    s="myOutput"; sed '$ /^'"$s"'$/b; s/$/n'"$s"'/' file


    s="myOutput" Set pattern to match
    sed use sed
    $ …' file On the last line of the file
    /^'"$s"'$/b Match the pattern as a whole line and branch to end if matched.
    s/$/n'"$s"'/ If no match, append the pattern at the end line.



    If the file already contains the "myOutput" data
    somewhere other than the last line,
    this will add another copy of it at the end of the file.






    share|improve this answer






















    • (1) Your answer doesn’t actually append the new line of text to the file (as the question requests and demonstrates) — you would need to use sed -i.  (2) Your command doesn’t work when the text string contains a slash.
      – Scott
      Jan 17 at 23:48














    up vote
    1
    down vote



    accepted










    Assuming that you want "myOutput" to be a full line
    at the last line of the file:



    s="myOutput"; sed '$ /^'"$s"'$/b; s/$/n'"$s"'/' file


    s="myOutput" Set pattern to match
    sed use sed
    $ …' file On the last line of the file
    /^'"$s"'$/b Match the pattern as a whole line and branch to end if matched.
    s/$/n'"$s"'/ If no match, append the pattern at the end line.



    If the file already contains the "myOutput" data
    somewhere other than the last line,
    this will add another copy of it at the end of the file.






    share|improve this answer






















    • (1) Your answer doesn’t actually append the new line of text to the file (as the question requests and demonstrates) — you would need to use sed -i.  (2) Your command doesn’t work when the text string contains a slash.
      – Scott
      Jan 17 at 23:48












    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    Assuming that you want "myOutput" to be a full line
    at the last line of the file:



    s="myOutput"; sed '$ /^'"$s"'$/b; s/$/n'"$s"'/' file


    s="myOutput" Set pattern to match
    sed use sed
    $ …' file On the last line of the file
    /^'"$s"'$/b Match the pattern as a whole line and branch to end if matched.
    s/$/n'"$s"'/ If no match, append the pattern at the end line.



    If the file already contains the "myOutput" data
    somewhere other than the last line,
    this will add another copy of it at the end of the file.






    share|improve this answer














    Assuming that you want "myOutput" to be a full line
    at the last line of the file:



    s="myOutput"; sed '$ /^'"$s"'$/b; s/$/n'"$s"'/' file


    s="myOutput" Set pattern to match
    sed use sed
    $ …' file On the last line of the file
    /^'"$s"'$/b Match the pattern as a whole line and branch to end if matched.
    s/$/n'"$s"'/ If no match, append the pattern at the end line.



    If the file already contains the "myOutput" data
    somewhere other than the last line,
    this will add another copy of it at the end of the file.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 17 at 23:47









    Scott

    6,27132347




    6,27132347










    answered Jan 15 at 1:38









    Isaac

    6,7611834




    6,7611834











    • (1) Your answer doesn’t actually append the new line of text to the file (as the question requests and demonstrates) — you would need to use sed -i.  (2) Your command doesn’t work when the text string contains a slash.
      – Scott
      Jan 17 at 23:48
















    • (1) Your answer doesn’t actually append the new line of text to the file (as the question requests and demonstrates) — you would need to use sed -i.  (2) Your command doesn’t work when the text string contains a slash.
      – Scott
      Jan 17 at 23:48















    (1) Your answer doesn’t actually append the new line of text to the file (as the question requests and demonstrates) — you would need to use sed -i.  (2) Your command doesn’t work when the text string contains a slash.
    – Scott
    Jan 17 at 23:48




    (1) Your answer doesn’t actually append the new line of text to the file (as the question requests and demonstrates) — you would need to use sed -i.  (2) Your command doesn’t work when the text string contains a slash.
    – Scott
    Jan 17 at 23:48












    up vote
    3
    down vote













    s="myOutput"; grep -Fxqe "$s" < "$file" || printf "%sn" "$s" >> "$file"





    share|improve this answer
























      up vote
      3
      down vote













      s="myOutput"; grep -Fxqe "$s" < "$file" || printf "%sn" "$s" >> "$file"





      share|improve this answer






















        up vote
        3
        down vote










        up vote
        3
        down vote









        s="myOutput"; grep -Fxqe "$s" < "$file" || printf "%sn" "$s" >> "$file"





        share|improve this answer












        s="myOutput"; grep -Fxqe "$s" < "$file" || printf "%sn" "$s" >> "$file"






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 14 at 22:54









        Stéphane Chazelas

        281k53518849




        281k53518849




















            up vote
            -1
            down vote













            In below script will check in complete last line for whether variable i present or not if not it inserts variable i in last line of file.



            As tested it worked fine



            #!bin/bash
            i="praveen"
            f=$(sed -n '$p' p.txt | sed -n "/^$i$/p")
            if [[ -z $f ]]
            then
            sed "$s/.*/&n$i/g" p.txt
            else
            echo "output exsists"
            fi





            share|improve this answer




















            • (1) The question says, “all in one line”.  You’ll notice that the earlier answers are one line long.  Yours is nine lines long (counting everything).  (2) You tested this?  It doesn’t work AT ALL because the she-bang is wrong: there needs to be a / at the beginning of the interpreter name (after the #!).  (3) s/$/nfoo/ (which isaac’s answer uses) is a somewhat awkward way to add a line of text (containing “foo”) to a file in sed. (As I’m sure you understand, … (Cont’d)
              – Scott
              Jan 17 at 23:44










            • (Cont’d) …  a $ at the beginning of that command (before the s) represents the last line of the file, and says “do this at the end of the file”.)  s/.*/&nfoo/ (which your answer uses) is an unnecessarily complicated version of the same command.  But isaac’s answer uses that awkward technique because he’s trying to solve the problem economically (i.e., in one line) and he’s using sed already. Your use of it in a standalone command (the sixth line of your script) is doubly unnecessarily complicated and unintuitive, because, in that context, you don’t need to be using sed. … (Cont’d)
              – Scott
              Jan 17 at 23:44










            • (Cont’d) …  It would be much more natural to do what the OP is already doing (in the question): cat >> "$file" <<< "$i" — or, better yet, something like Stéphane’s version: printf "%sn" "$i" >> "$file". (4) The question asks how to append a line of text to a file. Your answer doesn’t even accomplish that (although, to be fair, isaac’s doesn’t either). To append a line of text to a file using your command, you would need to use sed -i. (5) Your command doesn’t work when the text string contains a slash.
              – Scott
              Jan 17 at 23:44














            up vote
            -1
            down vote













            In below script will check in complete last line for whether variable i present or not if not it inserts variable i in last line of file.



            As tested it worked fine



            #!bin/bash
            i="praveen"
            f=$(sed -n '$p' p.txt | sed -n "/^$i$/p")
            if [[ -z $f ]]
            then
            sed "$s/.*/&n$i/g" p.txt
            else
            echo "output exsists"
            fi





            share|improve this answer




















            • (1) The question says, “all in one line”.  You’ll notice that the earlier answers are one line long.  Yours is nine lines long (counting everything).  (2) You tested this?  It doesn’t work AT ALL because the she-bang is wrong: there needs to be a / at the beginning of the interpreter name (after the #!).  (3) s/$/nfoo/ (which isaac’s answer uses) is a somewhat awkward way to add a line of text (containing “foo”) to a file in sed. (As I’m sure you understand, … (Cont’d)
              – Scott
              Jan 17 at 23:44










            • (Cont’d) …  a $ at the beginning of that command (before the s) represents the last line of the file, and says “do this at the end of the file”.)  s/.*/&nfoo/ (which your answer uses) is an unnecessarily complicated version of the same command.  But isaac’s answer uses that awkward technique because he’s trying to solve the problem economically (i.e., in one line) and he’s using sed already. Your use of it in a standalone command (the sixth line of your script) is doubly unnecessarily complicated and unintuitive, because, in that context, you don’t need to be using sed. … (Cont’d)
              – Scott
              Jan 17 at 23:44










            • (Cont’d) …  It would be much more natural to do what the OP is already doing (in the question): cat >> "$file" <<< "$i" — or, better yet, something like Stéphane’s version: printf "%sn" "$i" >> "$file". (4) The question asks how to append a line of text to a file. Your answer doesn’t even accomplish that (although, to be fair, isaac’s doesn’t either). To append a line of text to a file using your command, you would need to use sed -i. (5) Your command doesn’t work when the text string contains a slash.
              – Scott
              Jan 17 at 23:44












            up vote
            -1
            down vote










            up vote
            -1
            down vote









            In below script will check in complete last line for whether variable i present or not if not it inserts variable i in last line of file.



            As tested it worked fine



            #!bin/bash
            i="praveen"
            f=$(sed -n '$p' p.txt | sed -n "/^$i$/p")
            if [[ -z $f ]]
            then
            sed "$s/.*/&n$i/g" p.txt
            else
            echo "output exsists"
            fi





            share|improve this answer












            In below script will check in complete last line for whether variable i present or not if not it inserts variable i in last line of file.



            As tested it worked fine



            #!bin/bash
            i="praveen"
            f=$(sed -n '$p' p.txt | sed -n "/^$i$/p")
            if [[ -z $f ]]
            then
            sed "$s/.*/&n$i/g" p.txt
            else
            echo "output exsists"
            fi






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 15 at 10:03









            Praveen Kumar BS

            1,010128




            1,010128











            • (1) The question says, “all in one line”.  You’ll notice that the earlier answers are one line long.  Yours is nine lines long (counting everything).  (2) You tested this?  It doesn’t work AT ALL because the she-bang is wrong: there needs to be a / at the beginning of the interpreter name (after the #!).  (3) s/$/nfoo/ (which isaac’s answer uses) is a somewhat awkward way to add a line of text (containing “foo”) to a file in sed. (As I’m sure you understand, … (Cont’d)
              – Scott
              Jan 17 at 23:44










            • (Cont’d) …  a $ at the beginning of that command (before the s) represents the last line of the file, and says “do this at the end of the file”.)  s/.*/&nfoo/ (which your answer uses) is an unnecessarily complicated version of the same command.  But isaac’s answer uses that awkward technique because he’s trying to solve the problem economically (i.e., in one line) and he’s using sed already. Your use of it in a standalone command (the sixth line of your script) is doubly unnecessarily complicated and unintuitive, because, in that context, you don’t need to be using sed. … (Cont’d)
              – Scott
              Jan 17 at 23:44










            • (Cont’d) …  It would be much more natural to do what the OP is already doing (in the question): cat >> "$file" <<< "$i" — or, better yet, something like Stéphane’s version: printf "%sn" "$i" >> "$file". (4) The question asks how to append a line of text to a file. Your answer doesn’t even accomplish that (although, to be fair, isaac’s doesn’t either). To append a line of text to a file using your command, you would need to use sed -i. (5) Your command doesn’t work when the text string contains a slash.
              – Scott
              Jan 17 at 23:44
















            • (1) The question says, “all in one line”.  You’ll notice that the earlier answers are one line long.  Yours is nine lines long (counting everything).  (2) You tested this?  It doesn’t work AT ALL because the she-bang is wrong: there needs to be a / at the beginning of the interpreter name (after the #!).  (3) s/$/nfoo/ (which isaac’s answer uses) is a somewhat awkward way to add a line of text (containing “foo”) to a file in sed. (As I’m sure you understand, … (Cont’d)
              – Scott
              Jan 17 at 23:44










            • (Cont’d) …  a $ at the beginning of that command (before the s) represents the last line of the file, and says “do this at the end of the file”.)  s/.*/&nfoo/ (which your answer uses) is an unnecessarily complicated version of the same command.  But isaac’s answer uses that awkward technique because he’s trying to solve the problem economically (i.e., in one line) and he’s using sed already. Your use of it in a standalone command (the sixth line of your script) is doubly unnecessarily complicated and unintuitive, because, in that context, you don’t need to be using sed. … (Cont’d)
              – Scott
              Jan 17 at 23:44










            • (Cont’d) …  It would be much more natural to do what the OP is already doing (in the question): cat >> "$file" <<< "$i" — or, better yet, something like Stéphane’s version: printf "%sn" "$i" >> "$file". (4) The question asks how to append a line of text to a file. Your answer doesn’t even accomplish that (although, to be fair, isaac’s doesn’t either). To append a line of text to a file using your command, you would need to use sed -i. (5) Your command doesn’t work when the text string contains a slash.
              – Scott
              Jan 17 at 23:44















            (1) The question says, “all in one line”.  You’ll notice that the earlier answers are one line long.  Yours is nine lines long (counting everything).  (2) You tested this?  It doesn’t work AT ALL because the she-bang is wrong: there needs to be a / at the beginning of the interpreter name (after the #!).  (3) s/$/nfoo/ (which isaac’s answer uses) is a somewhat awkward way to add a line of text (containing “foo”) to a file in sed. (As I’m sure you understand, … (Cont’d)
            – Scott
            Jan 17 at 23:44




            (1) The question says, “all in one line”.  You’ll notice that the earlier answers are one line long.  Yours is nine lines long (counting everything).  (2) You tested this?  It doesn’t work AT ALL because the she-bang is wrong: there needs to be a / at the beginning of the interpreter name (after the #!).  (3) s/$/nfoo/ (which isaac’s answer uses) is a somewhat awkward way to add a line of text (containing “foo”) to a file in sed. (As I’m sure you understand, … (Cont’d)
            – Scott
            Jan 17 at 23:44












            (Cont’d) …  a $ at the beginning of that command (before the s) represents the last line of the file, and says “do this at the end of the file”.)  s/.*/&nfoo/ (which your answer uses) is an unnecessarily complicated version of the same command.  But isaac’s answer uses that awkward technique because he’s trying to solve the problem economically (i.e., in one line) and he’s using sed already. Your use of it in a standalone command (the sixth line of your script) is doubly unnecessarily complicated and unintuitive, because, in that context, you don’t need to be using sed. … (Cont’d)
            – Scott
            Jan 17 at 23:44




            (Cont’d) …  a $ at the beginning of that command (before the s) represents the last line of the file, and says “do this at the end of the file”.)  s/.*/&nfoo/ (which your answer uses) is an unnecessarily complicated version of the same command.  But isaac’s answer uses that awkward technique because he’s trying to solve the problem economically (i.e., in one line) and he’s using sed already. Your use of it in a standalone command (the sixth line of your script) is doubly unnecessarily complicated and unintuitive, because, in that context, you don’t need to be using sed. … (Cont’d)
            – Scott
            Jan 17 at 23:44












            (Cont’d) …  It would be much more natural to do what the OP is already doing (in the question): cat >> "$file" <<< "$i" — or, better yet, something like Stéphane’s version: printf "%sn" "$i" >> "$file". (4) The question asks how to append a line of text to a file. Your answer doesn’t even accomplish that (although, to be fair, isaac’s doesn’t either). To append a line of text to a file using your command, you would need to use sed -i. (5) Your command doesn’t work when the text string contains a slash.
            – Scott
            Jan 17 at 23:44




            (Cont’d) …  It would be much more natural to do what the OP is already doing (in the question): cat >> "$file" <<< "$i" — or, better yet, something like Stéphane’s version: printf "%sn" "$i" >> "$file". (4) The question asks how to append a line of text to a file. Your answer doesn’t even accomplish that (although, to be fair, isaac’s doesn’t either). To append a line of text to a file using your command, you would need to use sed -i. (5) Your command doesn’t work when the text string contains a slash.
            – Scott
            Jan 17 at 23:44












             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f417117%2fappend-herestring-data-to-a-file-if-it-doesnt-already-exist-in-that-file-all%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay