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.

4
  • 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
    Commented Mar 16, 2016 at 15:36
  • I tried my command | grep stackoverflow > ex.txt and it worked without the echo , why is this?
    – Jonathan
    Commented Mar 16, 2016 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
    Commented Mar 16, 2016 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
    Commented Mar 16, 2016 at 15:50

1 Answer 1

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"

1
  • I got it working. I used your first suggestion. I just took out 'echo' from my original attempt.
    – Jonathan
    Commented Mar 16, 2016 at 15:36

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.