I have a logfile that contains many ^H
and ^M
characters, as the process that produces this file updates a text based progress bar.
When using cat
the output is evaluated and appears human readable and concise. Below is an example output.
Epoch 11/120
4355/4355 [==============================] - ETA: 0s - loss: 0.0096
Epoch 00011: val_loss did not improve from 0.00992
4355/4355 [==============================] - 1220s 280ms/step - loss: 0.0096 - val_loss: 0.0100
However, the file itself is huge compared to what the actual printed text from cat
would suggest (70MB for around 900 lines) of text like above.
Below is a snippet of the actual text contained within the logfile.
1/Unknown - 0s 81us/step - loss: 0.5337^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^M 2/Unknown - 1s 438ms/step - loss: 0.5299^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^
H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^M 3/Unknown - 1s 386ms/step - loss: 0.5286^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^M 4/Unknown - 1s 357ms/step - loss: 0.5289^H^H^H^H^H^H^H^H^H^
H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^M 5/Unknown - 2s 339ms/step - loss: 0.5277^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^M 6/Unknown - 2s 327ms/
step - loss: 0.5258^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^M 7/Unknown - 2s 318ms/step - loss: 0.5250^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^
H^H^H^H^H^M 8/Unknown - 2s 312ms/step - loss: 0.5260^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^M 9/Unknown - 3s 307ms/step - loss: 0.5265^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^
H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^M 10/Unknown - 3s 303ms/step - loss: 0.5257^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^M 11/Unknown - 3s 299ms/step - loss: 0.5258^H^H^H^
Basically, I want to create a file that looks just like what cat
produces.
Here's a few things i've tried with limited success:
tr -d '\b\r' < logfile > new_file
removes all of the characters, but therefore leaves all of the undesired text.cat logfile > new_file
effectively just copies the file verbatim with no evaluation of the special characters.cat logfile | col -b > new_file
is pretty close, but does something odd on one of the repeated lines:
4355/4355 [==============================] - ETA: 0ss--loss::0.0096557
Epoch 00011: val_loss did not improve from 0.00992
4355/4355 [==============================] - 1220s 280ms/step - loss: 0.0096 - val_loss: 0.0100
Any help would be appreciated.
Thanks
cat
produces" – It's not whatcat
produces. It's what your terminal (terminal emulator) shows after it interprets^H
and such. Whatcat
does is exactly like incat logfile > new_file
, except there's the terminal instead ofnew_file
.cat
the file to the terminal and save the ultimate result. Example fortmux
: here. It seems "Save Output As…" in the "File" menu ofkonsole
can do this. Other terminal emulators may provide similar functionality. Notes: (1) You need to make sure in advance the scrollback buffer is large enough. (2) I guess some terminal emulators may generate output with newlines where they wrap lines due to their width.col -b
is for. You see a difference from when dumping the file to a terminal because backspace on your terminal when the cursor is at the beginning of the line happens to cause the cursor to move back to the end of the previous line, but try it on a terminal that doesn't such as the linux virtual consoles and you'll see the same (see thebw
terminfo capability)