I’m having trouble using sed to replace non-printable characters with other non-printable characters.
Specifically, I want sed to look for a line in a table starting with a TAB and ‘insert’ a BACKSPACE, essentially bringing the text from that line up to the previous line.
The reason for this is that I have a table split into columns, with some rows taking up multiple lines:
column1,row1 column2,row1,line1
column2,row1,line2
I’d like it to end up like this (ish)
column1,row1 column2,row1,line1 column2,row1,line2
(the spacings don’t matter)
I can’t seem to manage this though. I’m unsure if the expressions \t sand \b aren’t recognized, but they don’t seem to be in the way I’ve tried. I also can’t get echo commands with octal representations of these
These are the kind of things I’ve tried:
sed 's/^\t/\b/' file.txt newfile.txt
sed 's/^(`echo "\011"`)/`echo "\010"`/' file.txt newfile.txt
Any help would be appreciated.
I wouldn’t do this with sed. Sure, it’s possible to have sed store a line in a hold buffer, then read the next line and if it starts with whitespace, pop and print the hold buffer then print the current line … but the code will look like your modem just lost carrier.
I’d use awk.
What does this do?
{buf=buf $0}, adds the current line to a buffer namedbuf./^[^ \t]/ && buf{print buf;buf=""}(which is ignored for the first line becausebufhas not yet been set), printsbuffor any line that doesn’t start with whitespace (i.e. the start of a new output line). Then it resetsbuf.END{print buf}, prints any leftovers.