0

I want to read the screen log file for one the games I am hosting on a Linux (ubuntu) machine. I read it through a web interface and I only display 20 lines at a time as it would get too big for my own comfort. However, the log file keeps getting spammed with random characters. Note that it only happens to this specific game.

Here is an example:

;1m[34m[47m\[24d[m[39;49m[37m[40m[1d[0;1m[34m[47m|[24d[m[39;49m[37m[40m[1d[0;1m[34m[47m/[24d[m[39;49m[37m[40m[1d[0;1m[34
m[47m-[24d[m[39;49m[37m[40m[1d[0;1m[34m[47m\[24d[m[39;49m[37m[40m[1d[0;1m[34m[47m|[24d[m[39;49m[37m[40m[1d[0;1m[34m[47m/
[24d[m[39;49m[37m[40m[1d[0;1m[34m[47m-[24d[m[39;49m[37m[40m[1d[0;1m[34m[47m\[24d[m[39;49m[37m[40m[1d[0;1m[34m[47m|[24d[m
[39;49m[37m[40m[1d[0;1m[34m[47m/[1;58H[0m[30m[47m1[24;3H[m[39;49m[37m[40m[1d[0;1m[34m[47m-[24d[m[39;49m[37m[40m[1d[0;1m[
34m[47m\[24d[m[39;49m[37m[40m[1d[0;1m[34m[47m|[24d[m[39;49m[37m[40m[1d[0;1m[34m[47m/[24d[m[39;49m[37m[40m[1d[0;1m[34m[47
m-[24d[m[39;49m[37m[40m[1d[0;1m[34m[47m\[24d[m[39;49m[37m[40m[1d[0;1m[34m[47m|[24d[m[39;49m[37m[40m[1d[0;1m[34m[47m/[24d
[m[39;49m[37m[40m[1d[0;1m[34m[47m-[24d[m[39;49m[37m[40m[1d[0;1m[34m[47m\[24d[m[39;49m[37m[40m

Here is the code I am using right now. It removes the unnecessary white spaces, adds a new line after 120 characters and only displays the last 20 lines of the file.

tail /srv/screenlog.0 | awk '{$1=$1};1' | sed 's/[^[:graph:] ]\+//g' | sed -e "s/.\{120\}/&\n/g" | sed -ne':a;$p;N;20,$D;ba'

I tried playing with sed in order to remove those random characters but all I did was making it worse. I really want to know if there is a way to make a patterns out of those characters and only remove the specific patterns without affecting the rest of the file.

GMBeniamin
  • 119
  • 1
  • 6
  • 12
  • 2
    These look lik [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) to colourize text output. [This Q&A](https://stackoverflow.com/questions/17998978/removing-colors-from-output) might help. – Benjamin W. Feb 17 '19 at 02:26
  • 1
    The example shown displays an onscreen "spinner" – jhnc Feb 17 '19 at 18:24
  • This is the best I could come out with. I really need to make the extra characters go away. I tried playing with the examples on the link but I couldn't manage to do anything useful. – GMBeniamin Feb 17 '19 at 22:51

1 Answers1

1

Some linux distributions come with the utility colorize. If you insert it into your pipeline it may help, although it won't remove all the unwanted characters, just the escape codes:

tail /srv/screenlog.0 | colorize --clean-all | awk ...
jhnc
  • 11,310
  • 1
  • 9
  • 26
  • I am using Ubuntu 18.04. It is saying the colorize command is not found. – GMBeniamin Feb 18 '19 at 22:33
  • `apt install colorize` – jhnc Feb 18 '19 at 22:35
  • Made the command work. Now the output looks like this d|[24d[1d/[24d[1d-[24d[1d\[24d[1d|[24d[1d/[24d[1d-[24d[1d\[24d[1d|[24d[1d/[24d[1d-[24d[ – GMBeniamin Feb 18 '19 at 22:41
  • :-( Look like colorize can't deal with all the escape codes. Sorry. I think those ones may be "VPA" commands to move the cursor around the screen (http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf 8.3.158), interspersed with the spinner characters: `/ - | \ ` – jhnc Feb 18 '19 at 23:06
  • Well, the console of this game I want to monitor is pretty messy so no wonder it looks like this. I will try to make it work like it is right now. – GMBeniamin Feb 19 '19 at 20:06