I want to save the command line output data in a text file. I am running macOS High Sierra 10.13.6. The problem is, it has some graphical output which I don't want to store. What I need is, only the strings and their corresponding values. Could you help me please?

The shell command I am running is:

pwmetrics http://www.cnn.com --runs=1

And the output shown in the Terminal is:

enter image description here

This tool to generate web page performance metrics is open source. Here is the error I am getting while I try to save my code:

the error

And the o.txt file contains nothing.

The shell command I have tried so far are:

pwmetrics http://www.ieee.org --runs=2 &> /Users/ssr/Desktop/o.txt
pwmetrics http://www.ieee.org 2>/dev/null | tee /Users/ssr/Desktop/o.txt
pwmetrics http://www.ieee.org yes>/dev/null | tee /Users/ssr/Desktop/o.txt
  • Great question. Since it’s open source, there are easy ways to figure out these are ANSI escape sequences and quite a few options to quash them. Hopefully my answer gives you some direction or ideas to try. Especially the quick one of set term dumb and rerunning the command one time. – bmike Sep 11 '18 at 4:19

You’ve got several options with that tool.

  1. Output JSON and parse that
  2. Upload to Google Sheets and export as CSV the text you need
  3. Use a tool to strip the ANSI escape codes that prettify (in the opinion of some) and color the text - one package to strip ANSI is https://github.com/chalk/strip-ansi
  4. Use other tools to strip these out by piping the output or saving it to a file - https://unix.stackexchange.com/questions/4527/program-that-passes-stdin-to-stdout-with-color-codes-stripped or https://superuser.com/questions/380772/removing-ansi-color-codes-from-text-stream
  5. Trying to set your terminal type to TERM=dumb (like Can't plot with gnuplot on my Mac)
  6. forking that project and changing the code so the ANSI escape values are nothing. (Lines 4-8 here https://github.com/paulirish/pwmetrics/blob/master/lib/utils/messages.ts or perhaps a few lines down where those variables are used to construct the output)

I would also tell the program to write your output file so you get a clearer view without needing to debug file handling and pipes in bash (assuming that’s not your strength here today).

  • 1
    Thank you so much @bmike, it was a helpful explanation! – samm Sep 25 '18 at 2:12

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.