grep: input file 'X' is also the outputWhat are the shell's control and redirection operators?Search for...

Is this standard Japanese employment negotiations, or am I missing something?

How to factor a fourth degree polynomial

Do I need transit visa for Dublin?

Does the sensor of a dslr count the number of photons that hits it?

An easy way to solve this limit of a sum?

Multi-user CRUD: Valid, Problem, or Error?

Gory anime with pink haired girl escaping an asylum

Howto display unicode character u2026 in terminal mode in emacs

How do I talk to my wife about unrealistic expectations?

What is the maximum amount of diamond in one Minecraft game?

Why does this function pointer assignment work when assigned directly but not with the conditional operator?

Possibility to correct pitch from digital versions of records with the hole not centered

Examples of fluid (including air) being used to transmit digital data?

Wouldn't putting an electronic key inside a small Faraday cage render it completely useless?

Is there a standard definition of the "stall" phenomena?

My professor has told me he will be the corresponding author. Will it hurt my future career?

How to delete multiple process id of a single process?

Did William Shakespeare hide things in his writings?

Groups where no elements commute except for the trivial cases

Do Goblin tokens count as Goblins?

Park the computer

Bringing coumarin-containing liquor into the USA

What is the meaning of "prairie-dog" in this sentence?

Are "confidant" and "confident" homophones?



grep: input file 'X' is also the output


What are the shell's control and redirection operators?Search for string(s) in files and report four lines of context (match and three following)Redirecting output in the shell, permissionsstarting server with nohup and redirecting input, outputHow do I sort using the grep and sort commands?How to redirect per-CPU top output to file?How to grep a program's output but also echo the output normally?deleting 0d carriage return from .csv fileNeed to redirect input from a file and also from the ttyHow to split an output to two files with grep?Using jq within pipe chain produces no outputHow to redirect output to file to STDOUT?






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







7















I use ubuntu 14.4, and attempt to redirect the output of grep command to a file, but I keep getting this error:



grep: input file 'X' is also the output



I've searched for this issue and just found out that it was a bug in ubuntu 12.4 and there is not any describe about, can anybody help me to figure out this problem?



I run the following command:



grep -E -r -o -n r"%}(.*){%" > myfile









share|improve this question

























  • If you are trying grep pattern file > file then it doesn't work. You cannot use the same file as input and output for grep.

    – jimmij
    Oct 25 '14 at 21:29













  • i add my command ! thanks for hint , but when i try to use a file in other path the does not predicate it and i cant do that !

    – Kasrâmvd
    Oct 25 '14 at 21:34













  • for example with ../f i get this bash: ../f.txt: Permission denied

    – Kasrâmvd
    Oct 25 '14 at 21:37











  • show full command, what is your input file, or are you using the pipe?

    – jimmij
    Oct 25 '14 at 21:42






  • 1





    Now I understand that you are actually greping recursively the whole directory structure and want to append the result to the file which already exists in this structure. The problem is that shell (bash, zsh, whatever) first performs redirections and only then goes back to commands (grep in this example). It means that grep in command grep pattern file > file sees already empty file, so has nothing as input. However if you use >> instead of > then the file is not empty, but grep throws error anyhow as it may lead to recursive processing the same line (pattern) over and over again.

    – jimmij
    Oct 25 '14 at 22:22


















7















I use ubuntu 14.4, and attempt to redirect the output of grep command to a file, but I keep getting this error:



grep: input file 'X' is also the output



I've searched for this issue and just found out that it was a bug in ubuntu 12.4 and there is not any describe about, can anybody help me to figure out this problem?



I run the following command:



grep -E -r -o -n r"%}(.*){%" > myfile









share|improve this question

























  • If you are trying grep pattern file > file then it doesn't work. You cannot use the same file as input and output for grep.

    – jimmij
    Oct 25 '14 at 21:29













  • i add my command ! thanks for hint , but when i try to use a file in other path the does not predicate it and i cant do that !

    – Kasrâmvd
    Oct 25 '14 at 21:34













  • for example with ../f i get this bash: ../f.txt: Permission denied

    – Kasrâmvd
    Oct 25 '14 at 21:37











  • show full command, what is your input file, or are you using the pipe?

    – jimmij
    Oct 25 '14 at 21:42






  • 1





    Now I understand that you are actually greping recursively the whole directory structure and want to append the result to the file which already exists in this structure. The problem is that shell (bash, zsh, whatever) first performs redirections and only then goes back to commands (grep in this example). It means that grep in command grep pattern file > file sees already empty file, so has nothing as input. However if you use >> instead of > then the file is not empty, but grep throws error anyhow as it may lead to recursive processing the same line (pattern) over and over again.

    – jimmij
    Oct 25 '14 at 22:22














7












7








7


2






I use ubuntu 14.4, and attempt to redirect the output of grep command to a file, but I keep getting this error:



grep: input file 'X' is also the output



I've searched for this issue and just found out that it was a bug in ubuntu 12.4 and there is not any describe about, can anybody help me to figure out this problem?



I run the following command:



grep -E -r -o -n r"%}(.*){%" > myfile









share|improve this question
















I use ubuntu 14.4, and attempt to redirect the output of grep command to a file, but I keep getting this error:



grep: input file 'X' is also the output



I've searched for this issue and just found out that it was a bug in ubuntu 12.4 and there is not any describe about, can anybody help me to figure out this problem?



I run the following command:



grep -E -r -o -n r"%}(.*){%" > myfile






shell ubuntu command-line grep io-redirection






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 25 '14 at 23:18









jimmij

33.4k8 gold badges78 silver badges115 bronze badges




33.4k8 gold badges78 silver badges115 bronze badges










asked Oct 25 '14 at 21:24









KasrâmvdKasrâmvd

4941 gold badge5 silver badges15 bronze badges




4941 gold badge5 silver badges15 bronze badges













  • If you are trying grep pattern file > file then it doesn't work. You cannot use the same file as input and output for grep.

    – jimmij
    Oct 25 '14 at 21:29













  • i add my command ! thanks for hint , but when i try to use a file in other path the does not predicate it and i cant do that !

    – Kasrâmvd
    Oct 25 '14 at 21:34













  • for example with ../f i get this bash: ../f.txt: Permission denied

    – Kasrâmvd
    Oct 25 '14 at 21:37











  • show full command, what is your input file, or are you using the pipe?

    – jimmij
    Oct 25 '14 at 21:42






  • 1





    Now I understand that you are actually greping recursively the whole directory structure and want to append the result to the file which already exists in this structure. The problem is that shell (bash, zsh, whatever) first performs redirections and only then goes back to commands (grep in this example). It means that grep in command grep pattern file > file sees already empty file, so has nothing as input. However if you use >> instead of > then the file is not empty, but grep throws error anyhow as it may lead to recursive processing the same line (pattern) over and over again.

    – jimmij
    Oct 25 '14 at 22:22



















  • If you are trying grep pattern file > file then it doesn't work. You cannot use the same file as input and output for grep.

    – jimmij
    Oct 25 '14 at 21:29













  • i add my command ! thanks for hint , but when i try to use a file in other path the does not predicate it and i cant do that !

    – Kasrâmvd
    Oct 25 '14 at 21:34













  • for example with ../f i get this bash: ../f.txt: Permission denied

    – Kasrâmvd
    Oct 25 '14 at 21:37











  • show full command, what is your input file, or are you using the pipe?

    – jimmij
    Oct 25 '14 at 21:42






  • 1





    Now I understand that you are actually greping recursively the whole directory structure and want to append the result to the file which already exists in this structure. The problem is that shell (bash, zsh, whatever) first performs redirections and only then goes back to commands (grep in this example). It means that grep in command grep pattern file > file sees already empty file, so has nothing as input. However if you use >> instead of > then the file is not empty, but grep throws error anyhow as it may lead to recursive processing the same line (pattern) over and over again.

    – jimmij
    Oct 25 '14 at 22:22

















If you are trying grep pattern file > file then it doesn't work. You cannot use the same file as input and output for grep.

– jimmij
Oct 25 '14 at 21:29







If you are trying grep pattern file > file then it doesn't work. You cannot use the same file as input and output for grep.

– jimmij
Oct 25 '14 at 21:29















i add my command ! thanks for hint , but when i try to use a file in other path the does not predicate it and i cant do that !

– Kasrâmvd
Oct 25 '14 at 21:34







i add my command ! thanks for hint , but when i try to use a file in other path the does not predicate it and i cant do that !

– Kasrâmvd
Oct 25 '14 at 21:34















for example with ../f i get this bash: ../f.txt: Permission denied

– Kasrâmvd
Oct 25 '14 at 21:37





for example with ../f i get this bash: ../f.txt: Permission denied

– Kasrâmvd
Oct 25 '14 at 21:37













show full command, what is your input file, or are you using the pipe?

– jimmij
Oct 25 '14 at 21:42





show full command, what is your input file, or are you using the pipe?

– jimmij
Oct 25 '14 at 21:42




1




1





Now I understand that you are actually greping recursively the whole directory structure and want to append the result to the file which already exists in this structure. The problem is that shell (bash, zsh, whatever) first performs redirections and only then goes back to commands (grep in this example). It means that grep in command grep pattern file > file sees already empty file, so has nothing as input. However if you use >> instead of > then the file is not empty, but grep throws error anyhow as it may lead to recursive processing the same line (pattern) over and over again.

– jimmij
Oct 25 '14 at 22:22





Now I understand that you are actually greping recursively the whole directory structure and want to append the result to the file which already exists in this structure. The problem is that shell (bash, zsh, whatever) first performs redirections and only then goes back to commands (grep in this example). It means that grep in command grep pattern file > file sees already empty file, so has nothing as input. However if you use >> instead of > then the file is not empty, but grep throws error anyhow as it may lead to recursive processing the same line (pattern) over and over again.

– jimmij
Oct 25 '14 at 22:22










3 Answers
3






active

oldest

votes


















6














It is not possible to use the same file as input and output for grep.
You may consider the following alternatives:





  • temporary file



    grep pattern file > tmp_file
    mv tmp_file file



  • sed



    sed -i -n '/pattern/p' file



  • put whole file in the variable (not bright idea for large files)



    x=$(cat file); echo "$x" | grep pattern > file







share|improve this answer
























  • sed can be more portable sed -i '/pattern/!d' file

    – Costas
    Oct 25 '14 at 21:53













  • But in the case OP wants sed -i -n 's/.*(pattern).*/1/p' file

    – Costas
    Oct 25 '14 at 22:20











  • @Costas you are right, the question was edited and additional info added so one can polish sed syntax as well.

    – jimmij
    Oct 25 '14 at 22:35





















4














You can use the --exclude field in your grep command like so:



grep  --exclude=myfile  -Eron  r"%}(.*){%"  >  myfile





share|improve this answer



















  • 2





    Make sure myfile is unique as it only applies to the file's basename. You don't want to skip files with the same name in other directories.

    – Walf
    Mar 7 '17 at 0:45



















1














I found a way in bash:
cat >> IN_OUTPUT_FILE <<< "$(grep something IN_OUTPUT_FILE)"



The command in <<<"..." will be executed first.






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%2f164236%2fgrep-input-file-x-is-also-the-output%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    It is not possible to use the same file as input and output for grep.
    You may consider the following alternatives:





    • temporary file



      grep pattern file > tmp_file
      mv tmp_file file



    • sed



      sed -i -n '/pattern/p' file



    • put whole file in the variable (not bright idea for large files)



      x=$(cat file); echo "$x" | grep pattern > file







    share|improve this answer
























    • sed can be more portable sed -i '/pattern/!d' file

      – Costas
      Oct 25 '14 at 21:53













    • But in the case OP wants sed -i -n 's/.*(pattern).*/1/p' file

      – Costas
      Oct 25 '14 at 22:20











    • @Costas you are right, the question was edited and additional info added so one can polish sed syntax as well.

      – jimmij
      Oct 25 '14 at 22:35


















    6














    It is not possible to use the same file as input and output for grep.
    You may consider the following alternatives:





    • temporary file



      grep pattern file > tmp_file
      mv tmp_file file



    • sed



      sed -i -n '/pattern/p' file



    • put whole file in the variable (not bright idea for large files)



      x=$(cat file); echo "$x" | grep pattern > file







    share|improve this answer
























    • sed can be more portable sed -i '/pattern/!d' file

      – Costas
      Oct 25 '14 at 21:53













    • But in the case OP wants sed -i -n 's/.*(pattern).*/1/p' file

      – Costas
      Oct 25 '14 at 22:20











    • @Costas you are right, the question was edited and additional info added so one can polish sed syntax as well.

      – jimmij
      Oct 25 '14 at 22:35
















    6












    6








    6







    It is not possible to use the same file as input and output for grep.
    You may consider the following alternatives:





    • temporary file



      grep pattern file > tmp_file
      mv tmp_file file



    • sed



      sed -i -n '/pattern/p' file



    • put whole file in the variable (not bright idea for large files)



      x=$(cat file); echo "$x" | grep pattern > file







    share|improve this answer













    It is not possible to use the same file as input and output for grep.
    You may consider the following alternatives:





    • temporary file



      grep pattern file > tmp_file
      mv tmp_file file



    • sed



      sed -i -n '/pattern/p' file



    • put whole file in the variable (not bright idea for large files)



      x=$(cat file); echo "$x" | grep pattern > file








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 25 '14 at 21:45









    jimmijjimmij

    33.4k8 gold badges78 silver badges115 bronze badges




    33.4k8 gold badges78 silver badges115 bronze badges













    • sed can be more portable sed -i '/pattern/!d' file

      – Costas
      Oct 25 '14 at 21:53













    • But in the case OP wants sed -i -n 's/.*(pattern).*/1/p' file

      – Costas
      Oct 25 '14 at 22:20











    • @Costas you are right, the question was edited and additional info added so one can polish sed syntax as well.

      – jimmij
      Oct 25 '14 at 22:35





















    • sed can be more portable sed -i '/pattern/!d' file

      – Costas
      Oct 25 '14 at 21:53













    • But in the case OP wants sed -i -n 's/.*(pattern).*/1/p' file

      – Costas
      Oct 25 '14 at 22:20











    • @Costas you are right, the question was edited and additional info added so one can polish sed syntax as well.

      – jimmij
      Oct 25 '14 at 22:35



















    sed can be more portable sed -i '/pattern/!d' file

    – Costas
    Oct 25 '14 at 21:53







    sed can be more portable sed -i '/pattern/!d' file

    – Costas
    Oct 25 '14 at 21:53















    But in the case OP wants sed -i -n 's/.*(pattern).*/1/p' file

    – Costas
    Oct 25 '14 at 22:20





    But in the case OP wants sed -i -n 's/.*(pattern).*/1/p' file

    – Costas
    Oct 25 '14 at 22:20













    @Costas you are right, the question was edited and additional info added so one can polish sed syntax as well.

    – jimmij
    Oct 25 '14 at 22:35







    @Costas you are right, the question was edited and additional info added so one can polish sed syntax as well.

    – jimmij
    Oct 25 '14 at 22:35















    4














    You can use the --exclude field in your grep command like so:



    grep  --exclude=myfile  -Eron  r"%}(.*){%"  >  myfile





    share|improve this answer



















    • 2





      Make sure myfile is unique as it only applies to the file's basename. You don't want to skip files with the same name in other directories.

      – Walf
      Mar 7 '17 at 0:45
















    4














    You can use the --exclude field in your grep command like so:



    grep  --exclude=myfile  -Eron  r"%}(.*){%"  >  myfile





    share|improve this answer



















    • 2





      Make sure myfile is unique as it only applies to the file's basename. You don't want to skip files with the same name in other directories.

      – Walf
      Mar 7 '17 at 0:45














    4












    4








    4







    You can use the --exclude field in your grep command like so:



    grep  --exclude=myfile  -Eron  r"%}(.*){%"  >  myfile





    share|improve this answer













    You can use the --exclude field in your grep command like so:



    grep  --exclude=myfile  -Eron  r"%}(.*){%"  >  myfile






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 4 '15 at 13:23









    AaronDanielsonAaronDanielson

    1413 bronze badges




    1413 bronze badges








    • 2





      Make sure myfile is unique as it only applies to the file's basename. You don't want to skip files with the same name in other directories.

      – Walf
      Mar 7 '17 at 0:45














    • 2





      Make sure myfile is unique as it only applies to the file's basename. You don't want to skip files with the same name in other directories.

      – Walf
      Mar 7 '17 at 0:45








    2




    2





    Make sure myfile is unique as it only applies to the file's basename. You don't want to skip files with the same name in other directories.

    – Walf
    Mar 7 '17 at 0:45





    Make sure myfile is unique as it only applies to the file's basename. You don't want to skip files with the same name in other directories.

    – Walf
    Mar 7 '17 at 0:45











    1














    I found a way in bash:
    cat >> IN_OUTPUT_FILE <<< "$(grep something IN_OUTPUT_FILE)"



    The command in <<<"..." will be executed first.






    share|improve this answer




























      1














      I found a way in bash:
      cat >> IN_OUTPUT_FILE <<< "$(grep something IN_OUTPUT_FILE)"



      The command in <<<"..." will be executed first.






      share|improve this answer


























        1












        1








        1







        I found a way in bash:
        cat >> IN_OUTPUT_FILE <<< "$(grep something IN_OUTPUT_FILE)"



        The command in <<<"..." will be executed first.






        share|improve this answer













        I found a way in bash:
        cat >> IN_OUTPUT_FILE <<< "$(grep something IN_OUTPUT_FILE)"



        The command in <<<"..." will be executed first.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 54 mins ago









        osexp2003osexp2003

        1214 bronze badges




        1214 bronze badges






























            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%2f164236%2fgrep-input-file-x-is-also-the-output%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

            Hudson River Historic District Contents Geography History The district today Aesthetics Cultural...

            The number designs the writing. Feandra Aversely Definition: The act of ingrafting a sprig or shoot of one...

            Ayherre Geografie Demografie Externe links Navigatiemenu43° 23′ NB, 1° 15′ WL43° 23′ NB, 1°...