regexsed

Easiest way to collapse mkfs output (backspaces and spaces)


I am dumping the output of the mkfs into the log file, but it displays its process interactively, printing backspaces to get cursor back, then prints spaces to erase, then doing backspaces again and printing new message.

In log it looks like a mess. Here's what I figured out to collapse set of BSs into single space

# echo -e "AAABBB\x08\x08\x08\x08\x08\x08CCC" | sed -e 's/\(\x08\)\1\+/\1/g' -e 's/\(\x08\)/x/'
AAABBBxCCC

but is there better way to collapse sequences of BSs + spaces + BSs ... into single space character using regex? I can write the binary doing it, but this is imho overkill...

Example input would be

# echo -e "123\x08\x08\x08   \x08\x08\x08456    789"

to convert to

123 456    789

Update: at the beginning I said that output is by mkfs. Let me show you the example so that you see the charset.

mke2fs 1.47.0 (5-Feb-2023)
fs_types for mke2fs.conf resolution: 'ext4'
Discarding device blocks:       0/16463361572864/1646336               done                            
Filesystem label=data_volume
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
412080 inodes, 1646336 blocks
82316 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1686110208
51 block groups
32768 blocks per group, 32768 fragments per group
8080 inodes per group
Filesystem UUID: c27f71c7-2d9e-4a33-9d54-449ab3e2f378
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables:  0/51     done                            
Writing inode tables:  0/51     done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information:  0/51     done

backspaces are not displayed in the window above (they were removed after copy-paste).

enter image description here


Solution

  • Try this:

    sed -E 's/\x08+([[:space:]]+\x08+)*/ /g'
    

    e.g.:

    $ echo -e "123\x08\x08\x08   \x08\x08\x08456    789" | sed -E 's/\x08+([[:space:]]+\x08+)*/ /g'
    123 456    789
    

    That should convert all chains of backspace characters (possibly with spaces in the middle) to individual blanks.