7

For example:

cat a.txt
1 2
1 6    

{ cat $HOME/SANITY/file.txt | grep 1 >> $HOME/SANITY/new.txt } > /dev/null

cut -d' ' -f2

Now i don't want the results to be shown when running the script with this code.

8
  • Use grep -q 1. Commented Sep 4, 2018 at 15:26
  • 1
    You don't want the results of which command? You can redirect any command's stderr or stdout to /dev/null to get rid of it. Commented Sep 4, 2018 at 15:27
  • 1
    Use awk instead of this grep/cut combination. Commented Sep 4, 2018 at 15:37
  • 1
    The output of cat goes to grep, and the output of grep goes to new.txt. What results are you seeing go to the terminal instead? Commented Sep 4, 2018 at 15:37
  • 2
    A minimal reproducible example that shows the specific unwanted output going to the screen would be helpful. You aren't showing anything being emitted by the 2nd line (the one with the > /dev/null), so it's not clear what the problem is. Commented Sep 4, 2018 at 16:26

2 Answers 2

11

You can redirect the output. if you only use your_command > /dev/null only stdout will be redirected. If you want to remove the output of stderr as well, redirect stderr to stdout and stdout to /dev/null using:

your_command > /dev/null 2>&1

2>&1 will move stderr to the file descriptor of stdout.

Sign up to request clarification or add additional context in comments.

1 Comment

command is the name of an actual built-in command that's part of the shell -- not necessarily ideal as a placeholder name.
1

Simply use in your case :

grep 1 "$HOME/SANITY/file.txt" >> "$HOME/SANITY/new.txt"

And for general purpose :

command_foo_bar > /dev/null # or any other non special file

2 Comments

You can try same by putting in .sh file and run. This command will give output on the screen.
@AnkurVerma It won't show any matching lines, but it will show potential errors like if the file can't be found