Bash equivalent of sed command












-1















I'm writing a script in bash and i would like to know if there is an alternative way to write these sed commands (without using sed):



sed '1,11d;$d' "${SOTTOCARTELLA}"/file
sed '1,11!d' "${SOTTOCARTELLA}"/file
sed '1,11d' -i "${SOTTOCARTELLA}"/file1









share|improve this question


















  • 1





    Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself.

    – Cyrus
    Nov 24 '18 at 11:25






  • 1





    What are you trying to achieve that sed isn't doing for you? If I read those commands correctly you can easily achieve the same with head and tail, but I don't know why you wouldn't use a working solution.

    – l0b0
    Nov 24 '18 at 11:25













  • I know that this works, i am just curios about if there is an equivalent that can produce the same result as these sed commands i posted here. How could it be done with head and tail? thanks!

    – Paolo Amato
    Nov 24 '18 at 11:30






  • 1





    If you are using head and tail, that's not bash!

    – Stephen C
    Nov 24 '18 at 11:31
















-1















I'm writing a script in bash and i would like to know if there is an alternative way to write these sed commands (without using sed):



sed '1,11d;$d' "${SOTTOCARTELLA}"/file
sed '1,11!d' "${SOTTOCARTELLA}"/file
sed '1,11d' -i "${SOTTOCARTELLA}"/file1









share|improve this question


















  • 1





    Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself.

    – Cyrus
    Nov 24 '18 at 11:25






  • 1





    What are you trying to achieve that sed isn't doing for you? If I read those commands correctly you can easily achieve the same with head and tail, but I don't know why you wouldn't use a working solution.

    – l0b0
    Nov 24 '18 at 11:25













  • I know that this works, i am just curios about if there is an equivalent that can produce the same result as these sed commands i posted here. How could it be done with head and tail? thanks!

    – Paolo Amato
    Nov 24 '18 at 11:30






  • 1





    If you are using head and tail, that's not bash!

    – Stephen C
    Nov 24 '18 at 11:31














-1












-1








-1


0






I'm writing a script in bash and i would like to know if there is an alternative way to write these sed commands (without using sed):



sed '1,11d;$d' "${SOTTOCARTELLA}"/file
sed '1,11!d' "${SOTTOCARTELLA}"/file
sed '1,11d' -i "${SOTTOCARTELLA}"/file1









share|improve this question














I'm writing a script in bash and i would like to know if there is an alternative way to write these sed commands (without using sed):



sed '1,11d;$d' "${SOTTOCARTELLA}"/file
sed '1,11!d' "${SOTTOCARTELLA}"/file
sed '1,11d' -i "${SOTTOCARTELLA}"/file1






bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 11:20









Paolo AmatoPaolo Amato

11




11








  • 1





    Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself.

    – Cyrus
    Nov 24 '18 at 11:25






  • 1





    What are you trying to achieve that sed isn't doing for you? If I read those commands correctly you can easily achieve the same with head and tail, but I don't know why you wouldn't use a working solution.

    – l0b0
    Nov 24 '18 at 11:25













  • I know that this works, i am just curios about if there is an equivalent that can produce the same result as these sed commands i posted here. How could it be done with head and tail? thanks!

    – Paolo Amato
    Nov 24 '18 at 11:30






  • 1





    If you are using head and tail, that's not bash!

    – Stephen C
    Nov 24 '18 at 11:31














  • 1





    Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself.

    – Cyrus
    Nov 24 '18 at 11:25






  • 1





    What are you trying to achieve that sed isn't doing for you? If I read those commands correctly you can easily achieve the same with head and tail, but I don't know why you wouldn't use a working solution.

    – l0b0
    Nov 24 '18 at 11:25













  • I know that this works, i am just curios about if there is an equivalent that can produce the same result as these sed commands i posted here. How could it be done with head and tail? thanks!

    – Paolo Amato
    Nov 24 '18 at 11:30






  • 1





    If you are using head and tail, that's not bash!

    – Stephen C
    Nov 24 '18 at 11:31








1




1





Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself.

– Cyrus
Nov 24 '18 at 11:25





Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself.

– Cyrus
Nov 24 '18 at 11:25




1




1





What are you trying to achieve that sed isn't doing for you? If I read those commands correctly you can easily achieve the same with head and tail, but I don't know why you wouldn't use a working solution.

– l0b0
Nov 24 '18 at 11:25







What are you trying to achieve that sed isn't doing for you? If I read those commands correctly you can easily achieve the same with head and tail, but I don't know why you wouldn't use a working solution.

– l0b0
Nov 24 '18 at 11:25















I know that this works, i am just curios about if there is an equivalent that can produce the same result as these sed commands i posted here. How could it be done with head and tail? thanks!

– Paolo Amato
Nov 24 '18 at 11:30





I know that this works, i am just curios about if there is an equivalent that can produce the same result as these sed commands i posted here. How could it be done with head and tail? thanks!

– Paolo Amato
Nov 24 '18 at 11:30




1




1





If you are using head and tail, that's not bash!

– Stephen C
Nov 24 '18 at 11:31





If you are using head and tail, that's not bash!

– Stephen C
Nov 24 '18 at 11:31












1 Answer
1






active

oldest

votes


















1














With sed '1,11!d' "${SOTTOCARTELLA}"/file you are asking for the first 11 lines of the file;



With sed '1,11d' -i "${SOTTOCARTELLA}"/file1 you are asking for the entire file except for the first 11 lines.



If you don't want to use head, tail or other binaries as suggested, you can achieve the same options using read and some support variables.



For example, let's try sed '1,11!d' "${SOTTOCARTELLA}"/file.
You will need a start point and an end point (and of course, the file).



start=1
end=11
counter="$((start - 1))";
file="${SOTTOCARTELLA}/file"
exec 3<"${file}" ### Create file descriptor 3
while IFS= read -r line <&3; do ### Read file line by line
if [ "${counter}" -lt "${end}" ]; then ### If I'm in my "bundaries"
printf "%sn" "${line}" ### Print the line
fi
counter="$((counter + 1))"
done
exec 3>&- ### Close file descriptor 3


Note that this piece of code can be way better (E.G. adding a control on the counter in while condition), but this is the least you will need to understand two things:





  • sed, head, tails, awk, etc. are born in order to avoid to rewrite over and over again same routines and, also, for performance issues; this is why everyone, including me, will be telling you to use those.

  • This kind of codes are useful only for portability concerns, that's why I wrote this piece of code in a posix compliant way.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457602%2fbash-equivalent-of-sed-command%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    With sed '1,11!d' "${SOTTOCARTELLA}"/file you are asking for the first 11 lines of the file;



    With sed '1,11d' -i "${SOTTOCARTELLA}"/file1 you are asking for the entire file except for the first 11 lines.



    If you don't want to use head, tail or other binaries as suggested, you can achieve the same options using read and some support variables.



    For example, let's try sed '1,11!d' "${SOTTOCARTELLA}"/file.
    You will need a start point and an end point (and of course, the file).



    start=1
    end=11
    counter="$((start - 1))";
    file="${SOTTOCARTELLA}/file"
    exec 3<"${file}" ### Create file descriptor 3
    while IFS= read -r line <&3; do ### Read file line by line
    if [ "${counter}" -lt "${end}" ]; then ### If I'm in my "bundaries"
    printf "%sn" "${line}" ### Print the line
    fi
    counter="$((counter + 1))"
    done
    exec 3>&- ### Close file descriptor 3


    Note that this piece of code can be way better (E.G. adding a control on the counter in while condition), but this is the least you will need to understand two things:





    • sed, head, tails, awk, etc. are born in order to avoid to rewrite over and over again same routines and, also, for performance issues; this is why everyone, including me, will be telling you to use those.

    • This kind of codes are useful only for portability concerns, that's why I wrote this piece of code in a posix compliant way.






    share|improve this answer






























      1














      With sed '1,11!d' "${SOTTOCARTELLA}"/file you are asking for the first 11 lines of the file;



      With sed '1,11d' -i "${SOTTOCARTELLA}"/file1 you are asking for the entire file except for the first 11 lines.



      If you don't want to use head, tail or other binaries as suggested, you can achieve the same options using read and some support variables.



      For example, let's try sed '1,11!d' "${SOTTOCARTELLA}"/file.
      You will need a start point and an end point (and of course, the file).



      start=1
      end=11
      counter="$((start - 1))";
      file="${SOTTOCARTELLA}/file"
      exec 3<"${file}" ### Create file descriptor 3
      while IFS= read -r line <&3; do ### Read file line by line
      if [ "${counter}" -lt "${end}" ]; then ### If I'm in my "bundaries"
      printf "%sn" "${line}" ### Print the line
      fi
      counter="$((counter + 1))"
      done
      exec 3>&- ### Close file descriptor 3


      Note that this piece of code can be way better (E.G. adding a control on the counter in while condition), but this is the least you will need to understand two things:





      • sed, head, tails, awk, etc. are born in order to avoid to rewrite over and over again same routines and, also, for performance issues; this is why everyone, including me, will be telling you to use those.

      • This kind of codes are useful only for portability concerns, that's why I wrote this piece of code in a posix compliant way.






      share|improve this answer




























        1












        1








        1







        With sed '1,11!d' "${SOTTOCARTELLA}"/file you are asking for the first 11 lines of the file;



        With sed '1,11d' -i "${SOTTOCARTELLA}"/file1 you are asking for the entire file except for the first 11 lines.



        If you don't want to use head, tail or other binaries as suggested, you can achieve the same options using read and some support variables.



        For example, let's try sed '1,11!d' "${SOTTOCARTELLA}"/file.
        You will need a start point and an end point (and of course, the file).



        start=1
        end=11
        counter="$((start - 1))";
        file="${SOTTOCARTELLA}/file"
        exec 3<"${file}" ### Create file descriptor 3
        while IFS= read -r line <&3; do ### Read file line by line
        if [ "${counter}" -lt "${end}" ]; then ### If I'm in my "bundaries"
        printf "%sn" "${line}" ### Print the line
        fi
        counter="$((counter + 1))"
        done
        exec 3>&- ### Close file descriptor 3


        Note that this piece of code can be way better (E.G. adding a control on the counter in while condition), but this is the least you will need to understand two things:





        • sed, head, tails, awk, etc. are born in order to avoid to rewrite over and over again same routines and, also, for performance issues; this is why everyone, including me, will be telling you to use those.

        • This kind of codes are useful only for portability concerns, that's why I wrote this piece of code in a posix compliant way.






        share|improve this answer















        With sed '1,11!d' "${SOTTOCARTELLA}"/file you are asking for the first 11 lines of the file;



        With sed '1,11d' -i "${SOTTOCARTELLA}"/file1 you are asking for the entire file except for the first 11 lines.



        If you don't want to use head, tail or other binaries as suggested, you can achieve the same options using read and some support variables.



        For example, let's try sed '1,11!d' "${SOTTOCARTELLA}"/file.
        You will need a start point and an end point (and of course, the file).



        start=1
        end=11
        counter="$((start - 1))";
        file="${SOTTOCARTELLA}/file"
        exec 3<"${file}" ### Create file descriptor 3
        while IFS= read -r line <&3; do ### Read file line by line
        if [ "${counter}" -lt "${end}" ]; then ### If I'm in my "bundaries"
        printf "%sn" "${line}" ### Print the line
        fi
        counter="$((counter + 1))"
        done
        exec 3>&- ### Close file descriptor 3


        Note that this piece of code can be way better (E.G. adding a control on the counter in while condition), but this is the least you will need to understand two things:





        • sed, head, tails, awk, etc. are born in order to avoid to rewrite over and over again same routines and, also, for performance issues; this is why everyone, including me, will be telling you to use those.

        • This kind of codes are useful only for portability concerns, that's why I wrote this piece of code in a posix compliant way.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 24 '18 at 12:06

























        answered Nov 24 '18 at 11:58









        ingroxdingroxd

        5981624




        5981624
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457602%2fbash-equivalent-of-sed-command%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

            Tonle Sap (See)

            I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

            Guatemaltekische Davis-Cup-Mannschaft