admin管理员组

文章数量:1122826

as title - I want to convert tabs in an input file / stdin to asciii unit separators on stdout / redirected file.

All of the following have no effect

tr 0x09 0x1f
tr '0x09' '0x1f'
sed  's#0x09#0x1f#g'
sed s#0x09#0x1f#g

have no effect

as title - I want to convert tabs in an input file / stdin to asciii unit separators on stdout / redirected file.

All of the following have no effect

tr 0x09 0x1f
tr '0x09' '0x1f'
sed  's#0x09#0x1f#g'
sed s#0x09#0x1f#g

have no effect

Share Improve this question asked yesterday njamescouknjamescouk 1771 silver badge13 bronze badges 1
  • With GNU sed: sed 's#\x09#\x1f#g' – Cyrus Commented yesterday
Add a comment  | 

2 Answers 2

Reset to default 2

Neither tr nor sed understand 0x09 and 0x1f. In bash, you can use the $'...' quotes with C-style backslash escape sequences. This style understands both \t and \x... notations.

printf 'a\tb\n' | sed $'s/\t/\x1f/g'
printf 'a\tb\n' | tr '\t' $'\x1f'

With GNU tr:

tr '\t' '\37'

\t: horizontal tab

\37: unit separator (octal 37, hex 1f)

See: man tr

本文标签: convert tab to unit separator using sed or tr with bashStack Overflow