 | | From: | Adam Smith | | Subject: | Sed substitution of metacharacters in regex | | Date: | Fri, 07 Jan 2005 07:33:48 -0800 |
|
|
 | Hello,
Replacing a metacharacter with another, (a "|" with a tab "\t" ) in | delimited fields in a record
Neither ==> sed 's/\|/\\t/g' pmdat6a.tmp > pmdat6b.tmp
nor
sed 's/\|/\\t/g' pmdat6a.tmp > pmdat6b.tmp
works Creates "t" delimited fields in record instead
Thanks
-- Adam --
|
|
 | | From: | Ed Morton | | Subject: | Re: Sed substitution of metacharacters in regex | | Date: | Fri, 07 Jan 2005 09:34:33 -0600 |
|
|
 |
Adam Smith wrote:
> Hello, > > Replacing a metacharacter with another, (a "|" with a tab "\t" ) in | > delimited fields in a record > > Neither ==> > sed 's/\|/\\t/g' pmdat6a.tmp > pmdat6b.tmp
No need to escape the |, e.g.:
PS1> echo "a|b" | sed 's/|/ /' a b PS1> echo "a|b" | sed 's/|/\\t/' a\tb
Whichever you prefer...
Regards, Ed.
|
|
 | | From: | Adam Smith | | Subject: | Re: Sed substitution of metacharacters in regex | | Date: | Fri, 07 Jan 2005 08:33:26 -0800 |
|
|
 | Have I missed something here?
Ed Morton wrote:
> > > Adam Smith wrote: > >> Hello, >> >> Replacing a metacharacter with another, (a "|" with a tab "\t" ) in | >> delimited fields in a record >> >> Neither ==> >> sed 's/\|/\\t/g' pmdat6a.tmp > pmdat6b.tmp > > > No need to escape the |, e.g.: > > 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"
> Whichever you prefer... > > Regards, > > Ed.
|
|
 | | From: | Stephane CHAZELAS | | Subject: | Re: Sed substitution of metacharacters in regex | | 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 character in your shell, try .
If your shell supports the $'...' special quotes (ksh93, zsh, bash), try:
sed $'s/|/\t/g'
But beware "\t" is turned into a 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
|
|
 | | From: | Ed Morton | | Subject: | Re: Sed substitution of metacharacters in regex | | Date: | Fri, 07 Jan 2005 10:35:33 -0600 |
|
|
 |
Adam Smith wrote: > Have I missed something here?
Seems like it. See below.
> Ed Morton wrote:
>> 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"
Then use the first version, i.e. just insert a tab character in your sed command.
Ed.
|
|
 | | From: | Laurenz Albe | | Subject: | Re: Sed substitution of metacharacters in regex | | Date: | Fri, 7 Jan 2005 15:43:11 +0000 (UTC) |
|
|
 | In comp.unix.shell Adam Smith wrote: > Replacing a metacharacter with another, (a "|" with a tab "\t" ) in | > delimited fields in a record > > Neither ==> > sed 's/\|/\\t/g' pmdat6a.tmp > pmdat6b.tmp > > nor > > sed 's/\|/\\t/g' pmdat6a.tmp > pmdat6b.tmp > > works > Creates "t" delimited fields in record instead
Instead of writing \\t, insert a tab character to produce something like
sed -e 's/|/ /g'
Sometimes hitting the tab key is enough, on some shells typing Ctrl+V and then hitting the tab key works.
Yours, Laurenz Albe
|
|
 | | From: | Adam Smith | | Subject: | Re: Sed substitution of metacharacters in regex | | Date: | Fri, 07 Jan 2005 08:36:16 -0800 |
|
|
 | Except that the code is embedded in script being run by the cron daemon, no keyboard intervention !!!!
Laurenz Albe wrote:
> In comp.unix.shell Adam Smith wrote: > >>Replacing a metacharacter with another, (a "|" with a tab "\t" ) in | >>delimited fields in a record >> >>Neither ==> >>sed 's/\|/\\t/g' pmdat6a.tmp > pmdat6b.tmp >> >>nor >> >>sed 's/\|/\\t/g' pmdat6a.tmp > pmdat6b.tmp >> >>works >>Creates "t" delimited fields in record instead > > > Instead of writing \\t, insert a tab character to produce something like > > sed -e 's/|/ /g' > > Sometimes hitting the tab key is enough, on some shells typing > Ctrl+V and then hitting the tab key works. > > Yours, > Laurenz Albe
|
|
 | | From: | Ed Morton | | Subject: | Re: Sed substitution of metacharacters in regex | | Date: | Fri, 07 Jan 2005 10:37:37 -0600 |
|
|
 |
Adam Smith wrote:
> Except that the code is embedded in script being run by the cron daemon, > no keyboard intervention !!!!
No-one's suggesting keyboard intervention, just edit the script that cron is running to use a tab character in the sed command (see below if you're not sure how to do that). And please don't top-post (http://en.wikipedia.org/wiki/Top-posting).
Ed.
> Laurenz Albe wrote:
>> Instead of writing \\t, insert a tab character to produce something like >> >> sed -e 's/|/ /g' >> >> Sometimes hitting the tab key is enough, on some shells typing >> Ctrl+V and then hitting the tab key works. >> >> Yours, >> Laurenz Albe
|
|
 | | From: | Adam Smith | | Subject: | Re: Sed substitution of metacharacters in regex | | Date: | Fri, 07 Jan 2005 09:16:03 -0800 |
|
|
 |
Ed Morton wrote:
> > > Adam Smith wrote: > >> Except that the code is embedded in script being run by the cron >> daemon, no keyboard intervention !!!! > > > No-one's suggesting keyboard intervention, just edit the script that > cron is running to use a tab character in the sed command (see below if > you're not sure how to do that). And please don't top-post > (http://en.wikipedia.org/wiki/Top-posting). > > Ed. > >> Laurenz Albe wrote: > > > >>> Instead of writing \\t, insert a tab character to produce something like >>> >>> sed -e 's/|/ /g' >>> >>> Sometimes hitting the tab key is enough, on some shells typing >>> Ctrl+V and then hitting the tab key works. >>>
I'll be darn'd, never knew this - the Ctrl+V worked thanks
>>> Yours, >>> Laurenz Albe
|
|
 | | From: | Adam Smith | | Subject: | Re: Sed substitution of metacharacters in regex | | Date: | Fri, 07 Jan 2005 11:56:00 -0800 |
|
|
 |
Ed Morton wrote:
> > > Adam Smith wrote: > >> Except that the code is embedded in script being run by the cron >> daemon, no keyboard intervention !!!! > > > No-one's suggesting keyboard intervention, just edit the script that > cron is running to use a tab character in the sed command (see below if > you're not sure how to do that). And please don't top-post > (http://en.wikipedia.org/wiki/Top-posting). > > Ed. > >> Laurenz Albe wrote: > > > >>> Instead of writing \\t, insert a tab character to produce something like >>> >>> sed -e 's/|/ /g' >>> >>> Sometimes hitting the tab key is enough, on some shells typing >>> Ctrl+V and then hitting the tab key works. >>> In prior post I expressed surprise that this worked, but alas it was illusory All it did was to insert the number of 'white spaces' equivalent to one tab mark
>>> Yours, >>> Laurenz Albe
|
|