How can I write into a file the output of a command but also include the command at the beginning of a file?
eg:
grep -nrs 'blah' . >/home/ca/out.txt
so that the file shows
grep -nrs 'blah' . >/home/ca/out.txt
./something.cpp:1329: if(blah)
to leave a blank line between?
grep -nrs 'blah' . >/home/ca/out.txt
./something.cpp:1329: if(blah)
....
Thank you
You can store command line and result both in a file using bash -vc like this:
bash -vc "grep -nrs 'blah' ." >& /home/ca/out.txt
-v is for verbose mode in bash that outputs full command before executing it.-c is for running a command from command line>& is for redirecting both stdout and stderrAnother approach is store command line in an array:
arr=(grep -nrs 'blah' .)
{ printf "%q " "${arr[@]}"; echo; echo; "${arr[@]}"; } >& /home/ca/out.txt
You can use logsave (usage) to log the output together with a timestamp and the command, e.g.:
logsave -a output.txt ls
Saves the output of the ls command into output.txt:
Log of ls
Thu Jan 29 16:49:25 2015
[output of command]
Thu Jan 29 16:49:25 2015
----------------
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With