1

Let's do a simple example. This is what the standard output was printed on screen by Terminal.

Mac:~ usr$ brew list
gdbm        libidn2         node        python@2        wget
gettext     libunistring    openssl     readline        xz
icu4c       lz4             python      sqlite

However, when I redirect the standard output to a file, the column style is lost.

Mac:~ usr$ brew list | tee "$(date '+%Y-%m-%d-%H%M%S').log"
gdbm
gettext
icu4c
libidn2
libunistring
lz4
node
openssl
python
python@2
readline
sqlite
wget
xz

How can I preserve the standard output style in the output file exactly the same as shown on Terminal screen so that the log file looks pretty?

The log file is not further piped into any other program, function or loop for analysis, where outputting every element every line can be advantageous.

The log file is just for personal reference so I wish to keep it in a reader friendly format, something like IETF's 72 character width. Another purpose for seeking this fashion is that it takes less vertical space when I physically print the file out on a sheet of paper, and perhaps, I can also apply this on other occasions.

Assume Terminal has the default macOS Window Size: 80 Columns x 24 Rows.

Update:

As suggsted by this discussion, I can achieve the desired output by using script

script -q /dev/null brew list | perl -pe 's/\r\n/\n/g' > "$(date '+%Y-%m-%d-%H%M%S').log"

The output style will be preserved but use emacs or vim to open the log file. If I use BBEdit to open the log file, tab width will be visually adjusted --- which led to me to falsely think the command is not working.

Community
  • 1
  • 1
Bart Simpson
  • 749
  • 3
  • 7
  • 17
  • 1
    Your terminal does this based on its width. Your file cannot know at script-/runtime what width of terminal you will later use to read it. What width do you want your file to assume? – jeremysprofile Jul 06 '18 at 16:30
  • 1
    Agree with @Josh that this is a duplicate. And would add "DON'T DO THIS!" to your log files. Your future self, your future replacement, and anyone who needs to parse the logs will thank you if you just sit on your hands for a while. – keithpjolley Jul 06 '18 at 16:42
  • 3
    `[01;34m~/src/glibc $ ^[[00mls^M COPYING ^[[0m^[[01;34massert^[[0m ^[[01;34mdlfcn^[[0m` -- Your log file when preserving `ls` output exactly the same as a terminal. You can use `less -r` to allow ansi formatting when viewing. – that other guy Jul 06 '18 at 16:45
  • 1
    *nod*. Making logs look pretty is much, much less important than making those logs easy to programmatically consume. It's far easier to consume one line per element... which is exactly *why* `brew` uses that format when its output is not to a TTY. – Charles Duffy Jul 06 '18 at 17:18
  • @ that other guy. Can you explain your code. I don't understand what they do. Why do they contain ^[[, and carriage-return character, ^M? I've also tried the script command as suggested from other thread; however, the output log file contains a lot of garbage characters so I am still playing around with script to see what I can get. – Bart Simpson Jul 06 '18 at 18:53

0 Answers0