Bash - Process Backspace Control Character When Redirecting Output to File

Bash - process backspace control character when redirecting output to file

Thanks for your comments! I ended up piping the output of that program to AWK Script I linked in the question. I get a well-formed file in the end.

the_program | ./awk_crush.sh > output.txt

The only downside is that I get the output only once the program itself is finished, even though the initial output exceeds 5M and should be passed in the lesser chunks. I don't know the exact reason, perhaps AWK script waits for EOF on stdin. Either way, on more modern system I would use

stdbuf -oL the_program | ./awk_crush.sh > output.txt

to process the output line-by-line. I'm stuck on RHEL4 with expired support though, so I'm unable to use neither stdbuf nor unbuffer. I'll leave it as-is, it's fine too.

The contents of awk_crush.sh are based on this answer, except with ^H sequences (which are supposed to be ASCII 08 characters entered via VIM commands) replaced with escape sequence \b:

#!/usr/bin/awk -f
function crushify(data) {
while (data ~ /[^\b]\b/) {
gsub(/[^\b]\b/, "", data)
}
print data
}

crushify($0)

Basically, it replaces character before \b and \b itself with empty string, and repeats it while there are \b in the string - just what I needed. It doesn't care for other escape sequences though, but if it's necessary, there's a more complete SED solution by Thomas Dickey.

How to apply backspace characters within a text file (ideally in vim)

Turn on the 'paste' option (using :set paste), and then press dd i <CTRL-R> 1 <ESC> on each line that you want to apply the backspaces to. This also works if you delete multiple lines, or even the whole file.

The key here is that you are using <CTRL-R> 1 in insert mode to 'type out' the contents of register 1 (where your deleted lines just got put), and 'paste' option prevents Vim from using any mappings or abbreviations.

OS X: man output giving extra characters when redirected

From the OS X man page for man (man man):

TIPS

[...]

To get a plain text version of a man page, without backspaces and
underscores, try

# man foo | col -b > foo.mantxt

Applying this to your example would look like:

man open | col -b > open.txt

Echo Control C character

No, piping a CTRL-C character into your process won't work, because a CTRL-C keystroke is captured by the terminal and translated into a kill signal sent to the process.

The correct character code (in ASCII) for CTRL-C is code number 3 but, if you echo that to your program, it will simply receive the character from its standard input. It won't cause the process to terminate.

If you wanted to ease the task of finding and killing the process, you could use something like:

./program_that_does_not_terminate &
pid=$!
sleep 20
kill ${pid}

$! will give you the process ID for the last started background process.



Related Topics



Leave a reply



Submit