From: Ed Morton on 28 Feb 2010 23:06 On 2/28/2010 11:48 AM, August Karlstrom wrote: > Hi, > > How do I escape all meta characters in $foo in the command > > sed s/$foo/bar/ > Hang on, isn't that the same as asking "How do I replace one STRING (as opposed to a REGEXP) with another?"? It seems like all you want is just to have $foo treated as a string so instead of using a RE replacement mechanism like the above you could just use awk functions that operate on strings as opposed to REs, e.g.: awk -v old="$foo" -v new="bar" ' strt=index($0,old){ $0 = substr($0,1,strt-1) new substr($0,strt+length(old)) }1' Regards, Ed.
From: Ed Morton on 1 Mar 2010 14:48 On Feb 28, 3:51 pm, August Karlstrom <fusionf...(a)gmail.com> wrote: > Teemu Likonen wrote: > > * 2010-02-28 18:48 (+0100), August Karlstrom wrote: > > >> How do I escape all meta characters in $foo in the command > > >> sed s/$foo/bar/ > > > #!/bin/sh > > quote-regexp () { > > printf '%s\n' "$1" | sed -e 's,[]/[\^$.*],\\&,g' > > } > > > foo=something > > foo_escaped=$(quote-regexp "$foo") > > sed -e "s/$foo_escaped/bar/" > > > Note that the function escapes "/" character too because the character > > will be used in the final sed's s/// command. > > Thanks Teemu. I thought there would be a standard command for quoting > regular expressions. > > August- Hide quoted text - > > - Show quoted text - The standard way of not treating an expression as an RE is to not use RE operators on it (e.g. seds "//" or awks "*sub()" functions). Unfortunately I can't think of a counter-example but I don't think Teemu's approach of sticking a backslash in front of BRE metacharacters will work for all possible BREs and of course it certainly won't for EREs (which I think some seds support these days and all awks support). You should really consider just using a tool that can treat a string like a string such as the awk example I posted or, apparently, perl. Ed.
From: August Karlstrom on 2 Mar 2010 13:33 Ed Morton wrote: > Hang on, isn't that the same as asking "How do I replace one STRING (as > opposed to a REGEXP) with another?"? It seems like all you want is just > to have $foo treated as a string so instead of using a RE replacement > mechanism like the above you could just use awk functions that operate > on strings as opposed to REs, e.g.: > > awk -v old="$foo" -v new="bar" ' > strt=index($0,old){ $0 = substr($0,1,strt-1) new > substr($0,strt+length(old)) }1' Thanks Ed. I still find it hard to see why such a common task should require all that syntax. August
From: Janis Papanagnou on 2 Mar 2010 13:47 August Karlstrom wrote: > Ed Morton wrote: >> Hang on, isn't that the same as asking "How do I replace one STRING >> (as opposed to a REGEXP) with another?"? It seems like all you want is >> just to have $foo treated as a string so instead of using a RE >> replacement mechanism like the above you could just use awk functions >> that operate on strings as opposed to REs, e.g.: >> >> awk -v old="$foo" -v new="bar" ' >> strt=index($0,old){ $0 = substr($0,1,strt-1) new >> substr($0,strt+length(old)) }1' > > Thanks Ed. I still find it hard to see why such a common task should > require all that syntax. This is, actually, a task that I find not very well suported by awk. (Too many examples like that may repel people, I fear - well, with the exception of php programmers, maybe. ;-) Janis > > > August
From: Ed Morton on 2 Mar 2010 15:09 On Mar 2, 12:33 pm, August Karlstrom <fusionf...(a)gmail.com> wrote: > Ed Morton wrote: > > Hang on, isn't that the same as asking "How do I replace one STRING (as > > opposed to a REGEXP) with another?"? It seems like all you want is just > > to have $foo treated as a string so instead of using a RE replacement > > mechanism like the above you could just use awk functions that operate > > on strings as opposed to REs, e.g.: > > > awk -v old="$foo" -v new="bar" ' > > strt=index($0,old){ $0 = substr($0,1,strt-1) new > > substr($0,strt+length(old)) }1' > > Thanks Ed. I still find it hard to see why such a common task should > require all that syntax. > > August I agree. Its a mystery why just replacing one string with another should be harder to code than replacing an RE with a string. Sounds like perl may have done something about that but I don't know the details. Ed.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: search pattern and display lines around the pattern Next: Processing stdin in blocks |