Re: Sed substitution of metacharacters in regex
From: Stephane CHAZELAS (this.address_at_is.invalid)
Date: 01/07/05
- Next message: Adam Smith: "Re: Sed substitution of metacharacters in regex"
- Previous message: Ed Morton: "Re: Sed substitution of metacharacters in regex"
- In reply to: Adam Smith: "Re: Sed substitution of metacharacters in regex"
- Next in thread: Laurenz Albe: "Re: Sed substitution of metacharacters in regex"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Adam Smith: "Re: Sed substitution of metacharacters in regex"
- Previous message: Ed Morton: "Re: Sed substitution of metacharacters in regex"
- In reply to: Adam Smith: "Re: Sed substitution of metacharacters in regex"
- Next in thread: Laurenz Albe: "Re: Sed substitution of metacharacters in regex"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|