Re: Sed substitution of metacharacters in regex

From: Stephane CHAZELAS (this.address_at_is.invalid)
Date: 01/07/05


Date: Fri, 7 Jan 2005 16:59:04 +0000

2005-01-07, 08:33(-08), Adam Smith:
[...]
>> PS1> echo "a|b" | sed 's/|/ /'
>> a b
>> PS1> echo "a|b" | sed 's/|/\\t/'
>> a\tb
>>
> The \t is supposed to be a non-printable character for the tab, and
> that's what is needed to be inserted not the "\t"
[...]

On the right hand side of a substitution, \ is only used in \\,
\&, \1, \2..., \n, \/ (extension possibles with some sed
implementations).

The tabulation character may not be printable, it's still a
valid character in an argument to a command.

sed 's/|/ /g'
         ^^^^^^^
         tab character here

is a valid command.

If you have troubles entering a <Tab> character in your shell,
try <Ctrl-V><Tab>.

If your shell supports the $'...' special quotes (ksh93, zsh,
bash), try:

sed $'s/|/\t/g'

But beware "\t" is turned into a <Tab> character by the shell
before passing the string as an argument to sed.

Alternatively, you can have a variable per control character
this way:

eval "`echo \"SOH='a' STX='b' ETX='c' EOT='d' ENQ='e' ACK='f' \
BEL='g' BS='h' HT='i' LF='j' VT='k' FF='l' CR='m' SO='n' \
SI='o' DLE='p' DC1='q' DC2='r' DC3='s' DC4='t' NAK='u' \
SYN='v' ETB='w' CAN='x' EM='y' SUB='z' ESC='{' FS='|' GS='}' \
RS='~' US='@'\" | tr '[a-}]@' '[\1-\36]\37'`"

Now you can do:

sed "s/|/$HT/g"

(again, $HT is expanded by the shell, it's a real tab character
in the argument sed gets from the shell).

--
Stephane


Relevant Pages

  • Re: Passing tab char in command line
    ... I want to pass a tab character as a command line parameter. ... input to a program which is typically called a "shell". ...
    (comp.lang.java.programmer)
  • Re: tab character in MSH ?
    ... Is it possible to make MSH differentiate between a pasted and a typed ... I often debug snippets of shell code by having ... common indentation character should not turn into a ... Tab is *very* commonly used as a completion character almost everywhere, ...
    (microsoft.public.windows.server.scripting)
  • Re: Formatting index entries
    ... replace each space with a tab character ... Copy the whole cell ... use Edit>Replace to replace every tab with a space. ... you want to rip all the Index tags out of the main document. ...
    (microsoft.public.mac.office.word)
  • Re: Calculations Result Sometimes Displays 000, Sometimes 444
    ... > when I was addressing your mention of strange formatting options, ... the fill character for each tab was set to 4 on one of the ... added to a layout (not duplicate or copy / paste) will automatically ...
    (comp.databases.filemaker)
  • Re: Renaming documents from text within the document using existin
    ... StrName = ActiveDocument.Paragraphs.Range.Text ... character was found because you did trim off the first six characters. ... You can use the replace command to remove tab characters from a string and I ...
    (microsoft.public.word.vba.general)