I use ubuntu 14.4, and been attempting 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 run the following command:
grep -E -r -o -n r"%}(.*){%" > myfile
As the error states, it seems that somehow it’s interpreting the input and output as same name/obj. I searched but couldn’t find what exactly is the problem?!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
You can use the --exclude field in your grep command like so:
grep --exclude=myfile -Eron r"%}(.*){%" > myfile
Method 3
Simply do this
echo "$(grep pattern file)" > file
Method 4
With GNU find and GNU grep (as found on Ubuntu), you could do:
find . -type f ! -samefile myfile
-exec grep -Hon 'r%}(.*){%' {} + > myfile
Or to also look in symlinks to regular files (like some greps do with -r):
find -L . '(' ! -xtype l -o -prune ')' ! -samefile myfile -type f
-exec grep -Hon 'r%}(.*){%' {} + > myfile
! -samefile myfile will make sure myfile or any hardlink (or symlink in the one with -L) to it is not included in the list of files grep will look in.
Another option is to use moreutils‘s sponge:
grep -Hon 'r%}(.*){%' . | sponge myfile
sponge will delay writing to the file until eof on its stdin, so until grep has finished (note however that to do that, it stores the whole input in memory).
Method 5
I found a way in bash:
cat >> IN_OUTPUT_FILE <<< "$(grep something IN_OUTPUT_FILE)"
The command in <<<"..." will be executed first.
Method 6
That’s because grep will continue to look into that folder after the creation of myfile.
You can simply give it another path:
grep -E -r -o -n r"%}(.*){%" > /tmp/myfile
Method 7
Just use tee instead of redirecting:
grep pattern file | tee file
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0