0

I have a command, but I want to get the results into a .txt file I can open. How do I alter my command to allow the results to be put into a text file. I plan to transfer this .txt file to my local desktop.

my command | grep stackoverflow 

I have tried: echo my command | grep stackoverflow > ex.txt Although nothing comes up in the .txt file.

Thanks.

Eric Renouf
  • 18,141
  • 4
  • 49
  • 65
Jonathan
  • 103
  • 2
  • did you try `echo my command | grep stackoverflow > ex.txt` or `my command | grep stackoverflow > ex.txt`? The first one will search for `stackoverflow` in the text `my command` and as that doesn't match nothing will be in ex.txt. – Lucas Mar 16 '16 at 15:36
  • I tried `my command | grep stackoverflow > ex.txt` and it worked without the `echo` , why is this? – Jonathan Mar 16 '16 at 15:40
  • 2
    You need to understand the basics of command line parsing done by the shell. Short answer: If you run `echo my command` you are not running `my command` so you will not get its output. You are running `echo` and telling it to *print* the text "my command". – Lucas Mar 16 '16 at 15:44
  • @Lucas, thanks for the explanation, got it. Must run it and put that into a text file. Not put the actual text of the command into a file. – Jonathan Mar 16 '16 at 15:50

1 Answers1

0

Well, basically use output-redirection

my command|grep stackoverflow > file       #writes output to <file>
my command|grep stackoverflow >> file      #appends <file> with output
my command|grep stackoverflow|tee file     #writes output to <file> and still prints to stdout
my command|grep stackoverflow|tee -a file  #appends <file> with output and still prints to stdout

The pipe takes everything from stdout and gives it as input to the command that follows. So:

echo "this is a text" # prints "this is a text"
ls                    # prints the contents of the current directory

grep will now try to find a matching regular expression in the input it gets.

echo "my command" | grep stackoverflow  #will find no matching line.
echo "my command" | grep command        #will find a matching line.

I guess "my command" stands for a command, not for the message "my command"

Pierre.Vriens
  • 1,088
  • 21
  • 13
  • 16
dasBaschdi
  • 56
  • 2