0

How do I convert carriage returns from a text file to the way they are displayed in console?

So for example, if a program prints the following lines to a console:

start\n
progress  01%\r
progress  22%\r
progress  54%\r
progress 100%\n
completed

after a time the output in the console will look like this:

start
progress 100%\n
completed

but if I pipe the output to a file it will contain all the intermediate progress reports.

Preferably some solution that can be applied to large log files >100 MB.

Jaakko
  • 320
  • 3
  • 12
  • There is a program called "sed" which does search and replace (among other things), but won't find new line characters 'cos it works line by line. However, there is a programming language called perl that can also act as a command line program too, and it can do search and replace, so you could get it to delete \r characters. Some perl one liners mentioned in the accepted answer here https://superuser.com/questions/416419/perl-for-matching-with-regular-expressions-in-terminal – barlop Apr 12 '17 at 11:36
  • I'm pretty sure simple delete won't work because it will leave the intermediate strings `progress 22%` that would be overwritten otherwise because of the `\r` without `\n`. The below `col` answer already solved my issue but I couldn't yet (2 day timeout) mark it as the answer. – Jaakko Apr 12 '17 at 11:43
  • The words in your question could be better 'cos while your example shows some strings of text / lines, have been deleted, your words don't mention anything about deleting any lines. – barlop Apr 12 '17 at 12:10
  • If your subject was Remove lines with carriage returns from file, would that be a good description of what your goal was? – barlop Apr 12 '17 at 12:11
  • That's a different thing, because if first print is `1234\r` followed with `321\n` the end result in the console would be `3214\n`. If you remove lines with carriage returns, the result would be `321\n`. – Jaakko Apr 12 '17 at 12:35
  • What function would cause `1234\r321\n` to become `3214\n`? If it did that then t'd mean the 123 got reversed. Or the 123 at the beginning got deleted and the 4 moved to the end of the 321. But who said anything about reversing anything or moving anything? – barlop Apr 12 '17 at 12:52
  • For example in Ubuntu bash, `$ printf "1234\r321\n"` returns `3214\n` in console. Well at least it looks like that on the console, of course in reality the data was 1234\r321\n. And this is exactly the question, I'm not interested what the actual data is, but how it looks on the console, I want to save that without the overhead. – Jaakko Apr 12 '17 at 12:55

1 Answers1

1

Tool called col seems to do the trick

col -bp <filename.log > fileout.log

Jaakko
  • 320
  • 3
  • 12