-1

I'm using tr and sed command to replace text in my file like this tr '\n' ' ' < afile.txt | sed '$s/ $/\n/' and as discussed here. Though running that on a big file will get my console spammed with output replaced text.

So my need is to run the commands, but silence its output. My google search here and calling tr --help not helpful to me so I ask here.

Nam G VU
  • 491
  • 1
  • 6
  • 17
  • 2
    The only purpose of tr is to write transformed content from stdin to stdout. So why silence the output? – FloHe Jan 22 '17 at 10:50
  • 1
    You don't want to mute it, you want to redirect it. Have you noticed that the original file has not changed? – ctrl-alt-delor Jan 22 '17 at 11:40

2 Answers2

3

It's unlikely that you'd like to discard the output from the pipeline. It is more likely that you'd like to store it somewhere rather than having it flood your terminal.

I think this is what you're looking for:

$ tr '\n' ' ' < afile.txt | sed '$s/ $/\n/' >anotherfile.txt

This will put the result of the pipeline into the file anotherfile.txt rather than onto the terminal. You are then free to inspect it and to replace the original file with it (mv anotherfile.txt afile.txt) if this makes sense with what it is you're trying to achieve.

The > at the end of the pipeline is an output redirection that will redirect the standard output stream of sed into the specified file. It works in the "opposite way" of the input redirection < that is used earlier in the pipeline to send the contents of afile.txt into the standard input stream of tr.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
0

In general you can silence a command's output by

command 1>/dev/null

Stuff written to stderr is still displayed. If you also want to disable output to stderr, use:

command 1>/dev/null 2>&1

Even shorter:

command &>/dev/null

If you want to know more about redirecting file desccriptors, look for Redirecting Standard Output and Standard Error in the man-page of bash.

ctrl-alt-delor
  • 27,473
  • 9
  • 58
  • 102
FloHe
  • 790
  • 6
  • 12
  • 1
    In this specific case, sending the output to the bit bucket means throwing away the result of the operation. You might as well not run the command (`tr` and/or `sed`) at all. – Kusalananda Jan 22 '17 at 10:59
  • Why? If I run a compound statement like: `{ tr '\n' ' '| sed 's/\(.*\)/\1\n/' ; } /dev/null` I get nothing – FloHe Jan 22 '17 at 11:02
  • So why did you run it? Where's the result? – Kusalananda Jan 22 '17 at 11:06
  • He asked for silencing the output. This accomplishes that, though it is of no use in this special case :) – FloHe Jan 22 '17 at 11:07
  • 3
    Well, in that case, a shorter solution would have been to prefix the whole line with `#`. – Kusalananda Jan 22 '17 at 11:08
  • Kusalananda is on point here. Although this answer is technically correct, it probably will not help OP accomplish his REAL task. Maybe it is worth to elaborate more on input/output redirection or to point OP to some useful link. :) – Fiisch Jan 22 '17 at 11:08
  • @FloHe Yes you did. It's a comment. – Kusalananda Jan 22 '17 at 11:12
  • Oh, you mean commenting. – FloHe Jan 22 '17 at 11:39
  • 1
    The second example is wrong, I will fix it for you. it says direct fd2 to where fd1 is currently going (no change), then direct fd1 to /dev/null. – ctrl-alt-delor Jan 22 '17 at 11:46