Note that there are some explanatory texts on larger screens.

plurals
  1. POStrip color code from sed output
    primarykey
    data
    text
    <p>I was working on a little script that would do some logging for me from an asterisk machine. From the linux (centOS) command line, I can type:</p> <pre><code>asterisk -rx "transcoder show" </code></pre> <p>which provides the output I need to log. This output looks like this:</p> <pre><code>5/5 encoders/decoders of 120 channels are in use. </code></pre> <p>I wanted to write a line to a log file that would look like this:</p> <pre><code>YYYY-MM-DD HH-MM-SS #encoders #decoders </code></pre> <p>and then I'd log this every couple of minutes, and I'd have a history of how many channels I was using. So I wrote a little php script:</p> <pre><code>&lt;?php exec("asterisk -rx \"transcoder show\" | sed 's|\\([0-9]*\\)\/\\([0-9]*\\).*|\\1:\\2|'",$retval); $time = date('Y-m-d h:i:s'); $vals = explode(":",$retval[0]); $encoders = trim($vals[0]); $decoders = trim($vals[1]); $logString = "$time\t$encoders\t$decoders\n"; $fh = fopen('/root/script/trans.log','a'); fwrite($fh,$logString); fclose($fh); ?&gt; </code></pre> <p>Sorry, the code is a little funky there, theoretically I should have been able to output exactly what I needed from sed, but the code you see there is after I had changed it to try to address the problem you'll se below. In testing, I found the sed script worked fine from the command line; it output #encoders:#decoders as expected. But, when I ran the php script, it was writing lines like this to the log file:</p> <pre><code>2011-06-13 10:24:02 ^[[0;37;40m6 6 </code></pre> <p>I can't figure out where the ^[[0;37;40m is coming from. It's a color code, I understand, to set the color for displaying the text in the shell, but is that something sed is adding, or is it coming from the original output? How do I get rid of it? If I use an echo statement to print the variables $encoders and $decoders to the command line, there is no special color formatting, nor is there in the output from the original asterisk -rx command.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload