File's order got destroyed while grepping and echoing into for loop












0















I want to write the result of grep inside for loop into a file, keeping original file structure.
Original file:



$cat newtest1
some_text
/homes/myself/joz
ivan petrov - 20/10/19
new customer:
/homes/myself/silvia
john smith - 30/11/19
old customer:
some_text


I am using grep in for loop and getting the following:



$for i in `cat newtest1 | grep customer -B2`; do echo $i; done
/homes/myself/joz
ivan
petrov
-


etc.
File's structure got changed.
I tried printf - the same result.
Question: how to keep file's structure using grep in for loop?










share|improve this question























  • What's your intent with the for loop? Why not just the grep?

    – egmont
    Mar 12 at 8:58











  • For I want to use some ifs inside for loop.

    – Josef Klimuk
    Mar 12 at 9:06













  • This sounds like an XY problem - for example how to process 3-line records that are delimited by the string "customer:"

    – steeldriver
    Mar 12 at 12:00













  • The question was asked exactly about what I needed and the answer helped. It was meaningless for me to include if statements, since I had no question about if statements.

    – Josef Klimuk
    Mar 12 at 13:15


















0















I want to write the result of grep inside for loop into a file, keeping original file structure.
Original file:



$cat newtest1
some_text
/homes/myself/joz
ivan petrov - 20/10/19
new customer:
/homes/myself/silvia
john smith - 30/11/19
old customer:
some_text


I am using grep in for loop and getting the following:



$for i in `cat newtest1 | grep customer -B2`; do echo $i; done
/homes/myself/joz
ivan
petrov
-


etc.
File's structure got changed.
I tried printf - the same result.
Question: how to keep file's structure using grep in for loop?










share|improve this question























  • What's your intent with the for loop? Why not just the grep?

    – egmont
    Mar 12 at 8:58











  • For I want to use some ifs inside for loop.

    – Josef Klimuk
    Mar 12 at 9:06













  • This sounds like an XY problem - for example how to process 3-line records that are delimited by the string "customer:"

    – steeldriver
    Mar 12 at 12:00













  • The question was asked exactly about what I needed and the answer helped. It was meaningless for me to include if statements, since I had no question about if statements.

    – Josef Klimuk
    Mar 12 at 13:15
















0












0








0








I want to write the result of grep inside for loop into a file, keeping original file structure.
Original file:



$cat newtest1
some_text
/homes/myself/joz
ivan petrov - 20/10/19
new customer:
/homes/myself/silvia
john smith - 30/11/19
old customer:
some_text


I am using grep in for loop and getting the following:



$for i in `cat newtest1 | grep customer -B2`; do echo $i; done
/homes/myself/joz
ivan
petrov
-


etc.
File's structure got changed.
I tried printf - the same result.
Question: how to keep file's structure using grep in for loop?










share|improve this question














I want to write the result of grep inside for loop into a file, keeping original file structure.
Original file:



$cat newtest1
some_text
/homes/myself/joz
ivan petrov - 20/10/19
new customer:
/homes/myself/silvia
john smith - 30/11/19
old customer:
some_text


I am using grep in for loop and getting the following:



$for i in `cat newtest1 | grep customer -B2`; do echo $i; done
/homes/myself/joz
ivan
petrov
-


etc.
File's structure got changed.
I tried printf - the same result.
Question: how to keep file's structure using grep in for loop?







command-line grep echo






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 12 at 8:52









Josef KlimukJosef Klimuk

555117




555117













  • What's your intent with the for loop? Why not just the grep?

    – egmont
    Mar 12 at 8:58











  • For I want to use some ifs inside for loop.

    – Josef Klimuk
    Mar 12 at 9:06













  • This sounds like an XY problem - for example how to process 3-line records that are delimited by the string "customer:"

    – steeldriver
    Mar 12 at 12:00













  • The question was asked exactly about what I needed and the answer helped. It was meaningless for me to include if statements, since I had no question about if statements.

    – Josef Klimuk
    Mar 12 at 13:15





















  • What's your intent with the for loop? Why not just the grep?

    – egmont
    Mar 12 at 8:58











  • For I want to use some ifs inside for loop.

    – Josef Klimuk
    Mar 12 at 9:06













  • This sounds like an XY problem - for example how to process 3-line records that are delimited by the string "customer:"

    – steeldriver
    Mar 12 at 12:00













  • The question was asked exactly about what I needed and the answer helped. It was meaningless for me to include if statements, since I had no question about if statements.

    – Josef Klimuk
    Mar 12 at 13:15



















What's your intent with the for loop? Why not just the grep?

– egmont
Mar 12 at 8:58





What's your intent with the for loop? Why not just the grep?

– egmont
Mar 12 at 8:58













For I want to use some ifs inside for loop.

– Josef Klimuk
Mar 12 at 9:06







For I want to use some ifs inside for loop.

– Josef Klimuk
Mar 12 at 9:06















This sounds like an XY problem - for example how to process 3-line records that are delimited by the string "customer:"

– steeldriver
Mar 12 at 12:00







This sounds like an XY problem - for example how to process 3-line records that are delimited by the string "customer:"

– steeldriver
Mar 12 at 12:00















The question was asked exactly about what I needed and the answer helped. It was meaningless for me to include if statements, since I had no question about if statements.

– Josef Klimuk
Mar 12 at 13:15







The question was asked exactly about what I needed and the answer helped. It was meaningless for me to include if statements, since I had no question about if statements.

– Josef Klimuk
Mar 12 at 13:15












1 Answer
1






active

oldest

votes


















3














You need to quote variables and the result of command substitution to prevent word splitting:



for i in "$(<newtest1 grep customer -B2)"; do
echo "$i"
done


Of course to just output the matching lines you don’t need the for loop, but I suppose you want to do other things with the lines. In any case you don’t need the cat, you can let grep open the file with grep … newtest1 or let the shell open it and assign its content to grep’s stdin as I did above. The latter has a number of advantages explained in this great answer: When should I use input redirection?






share|improve this answer

























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    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%2faskubuntu.com%2fquestions%2f1124978%2ffiles-order-got-destroyed-while-grepping-and-echoing-into-for-loop%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









    3














    You need to quote variables and the result of command substitution to prevent word splitting:



    for i in "$(<newtest1 grep customer -B2)"; do
    echo "$i"
    done


    Of course to just output the matching lines you don’t need the for loop, but I suppose you want to do other things with the lines. In any case you don’t need the cat, you can let grep open the file with grep … newtest1 or let the shell open it and assign its content to grep’s stdin as I did above. The latter has a number of advantages explained in this great answer: When should I use input redirection?






    share|improve this answer






























      3














      You need to quote variables and the result of command substitution to prevent word splitting:



      for i in "$(<newtest1 grep customer -B2)"; do
      echo "$i"
      done


      Of course to just output the matching lines you don’t need the for loop, but I suppose you want to do other things with the lines. In any case you don’t need the cat, you can let grep open the file with grep … newtest1 or let the shell open it and assign its content to grep’s stdin as I did above. The latter has a number of advantages explained in this great answer: When should I use input redirection?






      share|improve this answer




























        3












        3








        3







        You need to quote variables and the result of command substitution to prevent word splitting:



        for i in "$(<newtest1 grep customer -B2)"; do
        echo "$i"
        done


        Of course to just output the matching lines you don’t need the for loop, but I suppose you want to do other things with the lines. In any case you don’t need the cat, you can let grep open the file with grep … newtest1 or let the shell open it and assign its content to grep’s stdin as I did above. The latter has a number of advantages explained in this great answer: When should I use input redirection?






        share|improve this answer















        You need to quote variables and the result of command substitution to prevent word splitting:



        for i in "$(<newtest1 grep customer -B2)"; do
        echo "$i"
        done


        Of course to just output the matching lines you don’t need the for loop, but I suppose you want to do other things with the lines. In any case you don’t need the cat, you can let grep open the file with grep … newtest1 or let the shell open it and assign its content to grep’s stdin as I did above. The latter has a number of advantages explained in this great answer: When should I use input redirection?







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 12 at 9:06

























        answered Mar 12 at 8:59









        dessertdessert

        24.6k672105




        24.6k672105






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Ask Ubuntu!


            • 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%2faskubuntu.com%2fquestions%2f1124978%2ffiles-order-got-destroyed-while-grepping-and-echoing-into-for-loop%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

            How did Captain America manage to do this?

            迪纳利

            南乌拉尔铁路局