How to copy the file while making changes to every line [bol]?












-2














I need to copy a file, so that a destination file has some specific string on beginning of each line, and it needs to be a bash one liner. So no script and loops, just bol.



bol - bash one liner



I personally need this done with command that uses grep program. I appreciate if you can solve it any way possible, I just don't have that much use of it, if not with grep.



EDIT: Okay, can't be done with grep, sed is okay.










share|improve this question




















  • 1




    sed is your friend.
    – ctrl-alt-delor
    Jan 18 '17 at 21:56










  • I have this is as an assignment on my college :D needs to be grep and bol
    – Scarass
    Jan 18 '17 at 21:56












  • What is "bol"??
    – Kusalananda
    Jan 18 '17 at 21:57










  • bol - bash one liner
    – Scarass
    Jan 18 '17 at 21:57










  • What's bol? never heard of it
    – Jeff Schaller
    Jan 18 '17 at 21:58
















-2














I need to copy a file, so that a destination file has some specific string on beginning of each line, and it needs to be a bash one liner. So no script and loops, just bol.



bol - bash one liner



I personally need this done with command that uses grep program. I appreciate if you can solve it any way possible, I just don't have that much use of it, if not with grep.



EDIT: Okay, can't be done with grep, sed is okay.










share|improve this question




















  • 1




    sed is your friend.
    – ctrl-alt-delor
    Jan 18 '17 at 21:56










  • I have this is as an assignment on my college :D needs to be grep and bol
    – Scarass
    Jan 18 '17 at 21:56












  • What is "bol"??
    – Kusalananda
    Jan 18 '17 at 21:57










  • bol - bash one liner
    – Scarass
    Jan 18 '17 at 21:57










  • What's bol? never heard of it
    – Jeff Schaller
    Jan 18 '17 at 21:58














-2












-2








-2







I need to copy a file, so that a destination file has some specific string on beginning of each line, and it needs to be a bash one liner. So no script and loops, just bol.



bol - bash one liner



I personally need this done with command that uses grep program. I appreciate if you can solve it any way possible, I just don't have that much use of it, if not with grep.



EDIT: Okay, can't be done with grep, sed is okay.










share|improve this question















I need to copy a file, so that a destination file has some specific string on beginning of each line, and it needs to be a bash one liner. So no script and loops, just bol.



bol - bash one liner



I personally need this done with command that uses grep program. I appreciate if you can solve it any way possible, I just don't have that much use of it, if not with grep.



EDIT: Okay, can't be done with grep, sed is okay.







text-processing command-line grep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 16 at 11:49









Rui F Ribeiro

38.9k1479129




38.9k1479129










asked Jan 18 '17 at 21:44









Scarass

13329




13329








  • 1




    sed is your friend.
    – ctrl-alt-delor
    Jan 18 '17 at 21:56










  • I have this is as an assignment on my college :D needs to be grep and bol
    – Scarass
    Jan 18 '17 at 21:56












  • What is "bol"??
    – Kusalananda
    Jan 18 '17 at 21:57










  • bol - bash one liner
    – Scarass
    Jan 18 '17 at 21:57










  • What's bol? never heard of it
    – Jeff Schaller
    Jan 18 '17 at 21:58














  • 1




    sed is your friend.
    – ctrl-alt-delor
    Jan 18 '17 at 21:56










  • I have this is as an assignment on my college :D needs to be grep and bol
    – Scarass
    Jan 18 '17 at 21:56












  • What is "bol"??
    – Kusalananda
    Jan 18 '17 at 21:57










  • bol - bash one liner
    – Scarass
    Jan 18 '17 at 21:57










  • What's bol? never heard of it
    – Jeff Schaller
    Jan 18 '17 at 21:58








1




1




sed is your friend.
– ctrl-alt-delor
Jan 18 '17 at 21:56




sed is your friend.
– ctrl-alt-delor
Jan 18 '17 at 21:56












I have this is as an assignment on my college :D needs to be grep and bol
– Scarass
Jan 18 '17 at 21:56






I have this is as an assignment on my college :D needs to be grep and bol
– Scarass
Jan 18 '17 at 21:56














What is "bol"??
– Kusalananda
Jan 18 '17 at 21:57




What is "bol"??
– Kusalananda
Jan 18 '17 at 21:57












bol - bash one liner
– Scarass
Jan 18 '17 at 21:57




bol - bash one liner
– Scarass
Jan 18 '17 at 21:57












What's bol? never heard of it
– Jeff Schaller
Jan 18 '17 at 21:58




What's bol? never heard of it
– Jeff Schaller
Jan 18 '17 at 21:58










4 Answers
4






active

oldest

votes


















3














$ sed 's/^/specific string/' input >output


You said you needed to use grep, okay...



$ sed 's/^/specific string/' input | grep . >output





share|improve this answer





















  • haha, that's funny, I meant just grep :)
    – Scarass
    Jan 18 '17 at 22:04










  • @Scarass The grep utility will print lines matching a pattern. It is not the tool for the job. Tell that to your teacher. Point them to this post.
    – Kusalananda
    Jan 18 '17 at 22:06










  • @Scarass Seriously. It's like telling a carpenter to construct a chair using a pencil as a tool.
    – Kusalananda
    Jan 18 '17 at 22:07










  • I just found I can use sed too, I mean I should use sed, grep doesn't work with it ofcourse, sry, I wasn't aware of it, sry for posting without being informed enough. Thanks.
    – Scarass
    Jan 18 '17 at 22:17








  • 1




    @Kusalananda I'm such a carpenter, see my answer. PS: and I'm not Scarass' teacher.
    – xhienne
    Jan 18 '17 at 23:00



















1














Without sed and with GNU grep, as requested:



grep --label="SPECIFIC STRING" --null -H ^ input_file.txt


PS: In case you wonder, no, this is not a serious answer






share|improve this answer























  • Haha, let us laugh together at your unserious question. :-)
    – Scarass
    Jan 18 '17 at 23:15



















0














Maybe this (untested):



cat file.txt | sed 's/^(.*)$/new text at line start1/g' > file-copy.txt


could also work with awk instead of sed






share|improve this answer























  • sry, forgot to state that it needs to use grep
    – Scarass
    Jan 18 '17 at 21:59










  • Then say it. By the way is this homework, 'cos you won't learn if we do it for you.
    – ctrl-alt-delor
    Jan 18 '17 at 22:01










  • or <file.txt sed -e 's/(.*)/new text at line start1/g' >file-copy.txt avoids extra process, and unnecessary use of cat.
    – ctrl-alt-delor
    Jan 18 '17 at 22:06



















0














Since you asked for bash, specifically, here's a one-liner (although a lengthy one):



{ while IFS= read -r line ;do printf "%s %sn" "SPECIAL" "$line" ; done < input.txt ; [ -n "$line" ] && printf "%s %sn" "SPECIAL"  "$line" ;  }


But of course far shorter way is via awk:



 awk '{print "SPECIAL ",$0}' input.txt





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%2f338459%2fhow-to-copy-the-file-while-making-changes-to-every-line-bol%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    $ sed 's/^/specific string/' input >output


    You said you needed to use grep, okay...



    $ sed 's/^/specific string/' input | grep . >output





    share|improve this answer





















    • haha, that's funny, I meant just grep :)
      – Scarass
      Jan 18 '17 at 22:04










    • @Scarass The grep utility will print lines matching a pattern. It is not the tool for the job. Tell that to your teacher. Point them to this post.
      – Kusalananda
      Jan 18 '17 at 22:06










    • @Scarass Seriously. It's like telling a carpenter to construct a chair using a pencil as a tool.
      – Kusalananda
      Jan 18 '17 at 22:07










    • I just found I can use sed too, I mean I should use sed, grep doesn't work with it ofcourse, sry, I wasn't aware of it, sry for posting without being informed enough. Thanks.
      – Scarass
      Jan 18 '17 at 22:17








    • 1




      @Kusalananda I'm such a carpenter, see my answer. PS: and I'm not Scarass' teacher.
      – xhienne
      Jan 18 '17 at 23:00
















    3














    $ sed 's/^/specific string/' input >output


    You said you needed to use grep, okay...



    $ sed 's/^/specific string/' input | grep . >output





    share|improve this answer





















    • haha, that's funny, I meant just grep :)
      – Scarass
      Jan 18 '17 at 22:04










    • @Scarass The grep utility will print lines matching a pattern. It is not the tool for the job. Tell that to your teacher. Point them to this post.
      – Kusalananda
      Jan 18 '17 at 22:06










    • @Scarass Seriously. It's like telling a carpenter to construct a chair using a pencil as a tool.
      – Kusalananda
      Jan 18 '17 at 22:07










    • I just found I can use sed too, I mean I should use sed, grep doesn't work with it ofcourse, sry, I wasn't aware of it, sry for posting without being informed enough. Thanks.
      – Scarass
      Jan 18 '17 at 22:17








    • 1




      @Kusalananda I'm such a carpenter, see my answer. PS: and I'm not Scarass' teacher.
      – xhienne
      Jan 18 '17 at 23:00














    3












    3








    3






    $ sed 's/^/specific string/' input >output


    You said you needed to use grep, okay...



    $ sed 's/^/specific string/' input | grep . >output





    share|improve this answer












    $ sed 's/^/specific string/' input >output


    You said you needed to use grep, okay...



    $ sed 's/^/specific string/' input | grep . >output






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 18 '17 at 22:03









    Kusalananda

    121k16229372




    121k16229372












    • haha, that's funny, I meant just grep :)
      – Scarass
      Jan 18 '17 at 22:04










    • @Scarass The grep utility will print lines matching a pattern. It is not the tool for the job. Tell that to your teacher. Point them to this post.
      – Kusalananda
      Jan 18 '17 at 22:06










    • @Scarass Seriously. It's like telling a carpenter to construct a chair using a pencil as a tool.
      – Kusalananda
      Jan 18 '17 at 22:07










    • I just found I can use sed too, I mean I should use sed, grep doesn't work with it ofcourse, sry, I wasn't aware of it, sry for posting without being informed enough. Thanks.
      – Scarass
      Jan 18 '17 at 22:17








    • 1




      @Kusalananda I'm such a carpenter, see my answer. PS: and I'm not Scarass' teacher.
      – xhienne
      Jan 18 '17 at 23:00


















    • haha, that's funny, I meant just grep :)
      – Scarass
      Jan 18 '17 at 22:04










    • @Scarass The grep utility will print lines matching a pattern. It is not the tool for the job. Tell that to your teacher. Point them to this post.
      – Kusalananda
      Jan 18 '17 at 22:06










    • @Scarass Seriously. It's like telling a carpenter to construct a chair using a pencil as a tool.
      – Kusalananda
      Jan 18 '17 at 22:07










    • I just found I can use sed too, I mean I should use sed, grep doesn't work with it ofcourse, sry, I wasn't aware of it, sry for posting without being informed enough. Thanks.
      – Scarass
      Jan 18 '17 at 22:17








    • 1




      @Kusalananda I'm such a carpenter, see my answer. PS: and I'm not Scarass' teacher.
      – xhienne
      Jan 18 '17 at 23:00
















    haha, that's funny, I meant just grep :)
    – Scarass
    Jan 18 '17 at 22:04




    haha, that's funny, I meant just grep :)
    – Scarass
    Jan 18 '17 at 22:04












    @Scarass The grep utility will print lines matching a pattern. It is not the tool for the job. Tell that to your teacher. Point them to this post.
    – Kusalananda
    Jan 18 '17 at 22:06




    @Scarass The grep utility will print lines matching a pattern. It is not the tool for the job. Tell that to your teacher. Point them to this post.
    – Kusalananda
    Jan 18 '17 at 22:06












    @Scarass Seriously. It's like telling a carpenter to construct a chair using a pencil as a tool.
    – Kusalananda
    Jan 18 '17 at 22:07




    @Scarass Seriously. It's like telling a carpenter to construct a chair using a pencil as a tool.
    – Kusalananda
    Jan 18 '17 at 22:07












    I just found I can use sed too, I mean I should use sed, grep doesn't work with it ofcourse, sry, I wasn't aware of it, sry for posting without being informed enough. Thanks.
    – Scarass
    Jan 18 '17 at 22:17






    I just found I can use sed too, I mean I should use sed, grep doesn't work with it ofcourse, sry, I wasn't aware of it, sry for posting without being informed enough. Thanks.
    – Scarass
    Jan 18 '17 at 22:17






    1




    1




    @Kusalananda I'm such a carpenter, see my answer. PS: and I'm not Scarass' teacher.
    – xhienne
    Jan 18 '17 at 23:00




    @Kusalananda I'm such a carpenter, see my answer. PS: and I'm not Scarass' teacher.
    – xhienne
    Jan 18 '17 at 23:00













    1














    Without sed and with GNU grep, as requested:



    grep --label="SPECIFIC STRING" --null -H ^ input_file.txt


    PS: In case you wonder, no, this is not a serious answer






    share|improve this answer























    • Haha, let us laugh together at your unserious question. :-)
      – Scarass
      Jan 18 '17 at 23:15
















    1














    Without sed and with GNU grep, as requested:



    grep --label="SPECIFIC STRING" --null -H ^ input_file.txt


    PS: In case you wonder, no, this is not a serious answer






    share|improve this answer























    • Haha, let us laugh together at your unserious question. :-)
      – Scarass
      Jan 18 '17 at 23:15














    1












    1








    1






    Without sed and with GNU grep, as requested:



    grep --label="SPECIFIC STRING" --null -H ^ input_file.txt


    PS: In case you wonder, no, this is not a serious answer






    share|improve this answer














    Without sed and with GNU grep, as requested:



    grep --label="SPECIFIC STRING" --null -H ^ input_file.txt


    PS: In case you wonder, no, this is not a serious answer







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 18 '17 at 23:02

























    answered Jan 18 '17 at 22:56









    xhienne

    12k2654




    12k2654












    • Haha, let us laugh together at your unserious question. :-)
      – Scarass
      Jan 18 '17 at 23:15


















    • Haha, let us laugh together at your unserious question. :-)
      – Scarass
      Jan 18 '17 at 23:15
















    Haha, let us laugh together at your unserious question. :-)
    – Scarass
    Jan 18 '17 at 23:15




    Haha, let us laugh together at your unserious question. :-)
    – Scarass
    Jan 18 '17 at 23:15











    0














    Maybe this (untested):



    cat file.txt | sed 's/^(.*)$/new text at line start1/g' > file-copy.txt


    could also work with awk instead of sed






    share|improve this answer























    • sry, forgot to state that it needs to use grep
      – Scarass
      Jan 18 '17 at 21:59










    • Then say it. By the way is this homework, 'cos you won't learn if we do it for you.
      – ctrl-alt-delor
      Jan 18 '17 at 22:01










    • or <file.txt sed -e 's/(.*)/new text at line start1/g' >file-copy.txt avoids extra process, and unnecessary use of cat.
      – ctrl-alt-delor
      Jan 18 '17 at 22:06
















    0














    Maybe this (untested):



    cat file.txt | sed 's/^(.*)$/new text at line start1/g' > file-copy.txt


    could also work with awk instead of sed






    share|improve this answer























    • sry, forgot to state that it needs to use grep
      – Scarass
      Jan 18 '17 at 21:59










    • Then say it. By the way is this homework, 'cos you won't learn if we do it for you.
      – ctrl-alt-delor
      Jan 18 '17 at 22:01










    • or <file.txt sed -e 's/(.*)/new text at line start1/g' >file-copy.txt avoids extra process, and unnecessary use of cat.
      – ctrl-alt-delor
      Jan 18 '17 at 22:06














    0












    0








    0






    Maybe this (untested):



    cat file.txt | sed 's/^(.*)$/new text at line start1/g' > file-copy.txt


    could also work with awk instead of sed






    share|improve this answer














    Maybe this (untested):



    cat file.txt | sed 's/^(.*)$/new text at line start1/g' > file-copy.txt


    could also work with awk instead of sed







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 18 '17 at 22:05









    ctrl-alt-delor

    10.8k41957




    10.8k41957










    answered Jan 18 '17 at 21:57









    Timothy Truckle

    19016




    19016












    • sry, forgot to state that it needs to use grep
      – Scarass
      Jan 18 '17 at 21:59










    • Then say it. By the way is this homework, 'cos you won't learn if we do it for you.
      – ctrl-alt-delor
      Jan 18 '17 at 22:01










    • or <file.txt sed -e 's/(.*)/new text at line start1/g' >file-copy.txt avoids extra process, and unnecessary use of cat.
      – ctrl-alt-delor
      Jan 18 '17 at 22:06


















    • sry, forgot to state that it needs to use grep
      – Scarass
      Jan 18 '17 at 21:59










    • Then say it. By the way is this homework, 'cos you won't learn if we do it for you.
      – ctrl-alt-delor
      Jan 18 '17 at 22:01










    • or <file.txt sed -e 's/(.*)/new text at line start1/g' >file-copy.txt avoids extra process, and unnecessary use of cat.
      – ctrl-alt-delor
      Jan 18 '17 at 22:06
















    sry, forgot to state that it needs to use grep
    – Scarass
    Jan 18 '17 at 21:59




    sry, forgot to state that it needs to use grep
    – Scarass
    Jan 18 '17 at 21:59












    Then say it. By the way is this homework, 'cos you won't learn if we do it for you.
    – ctrl-alt-delor
    Jan 18 '17 at 22:01




    Then say it. By the way is this homework, 'cos you won't learn if we do it for you.
    – ctrl-alt-delor
    Jan 18 '17 at 22:01












    or <file.txt sed -e 's/(.*)/new text at line start1/g' >file-copy.txt avoids extra process, and unnecessary use of cat.
    – ctrl-alt-delor
    Jan 18 '17 at 22:06




    or <file.txt sed -e 's/(.*)/new text at line start1/g' >file-copy.txt avoids extra process, and unnecessary use of cat.
    – ctrl-alt-delor
    Jan 18 '17 at 22:06











    0














    Since you asked for bash, specifically, here's a one-liner (although a lengthy one):



    { while IFS= read -r line ;do printf "%s %sn" "SPECIAL" "$line" ; done < input.txt ; [ -n "$line" ] && printf "%s %sn" "SPECIAL"  "$line" ;  }


    But of course far shorter way is via awk:



     awk '{print "SPECIAL ",$0}' input.txt





    share|improve this answer


























      0














      Since you asked for bash, specifically, here's a one-liner (although a lengthy one):



      { while IFS= read -r line ;do printf "%s %sn" "SPECIAL" "$line" ; done < input.txt ; [ -n "$line" ] && printf "%s %sn" "SPECIAL"  "$line" ;  }


      But of course far shorter way is via awk:



       awk '{print "SPECIAL ",$0}' input.txt





      share|improve this answer
























        0












        0








        0






        Since you asked for bash, specifically, here's a one-liner (although a lengthy one):



        { while IFS= read -r line ;do printf "%s %sn" "SPECIAL" "$line" ; done < input.txt ; [ -n "$line" ] && printf "%s %sn" "SPECIAL"  "$line" ;  }


        But of course far shorter way is via awk:



         awk '{print "SPECIAL ",$0}' input.txt





        share|improve this answer












        Since you asked for bash, specifically, here's a one-liner (although a lengthy one):



        { while IFS= read -r line ;do printf "%s %sn" "SPECIAL" "$line" ; done < input.txt ; [ -n "$line" ] && printf "%s %sn" "SPECIAL"  "$line" ;  }


        But of course far shorter way is via awk:



         awk '{print "SPECIAL ",$0}' input.txt






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 18 '17 at 22:17









        Sergiy Kolodyazhnyy

        8,27212152




        8,27212152






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f338459%2fhow-to-copy-the-file-while-making-changes-to-every-line-bol%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

            Morgemoulin

            Scott Moir

            Souastre