Re: replacing substring starting with a % sign
From: laura fairhead (laura_fairhead_at_INVALID.com)
Date: 11/06/03
- Next message: laura fairhead: "Re: deleting file with special character in name"
- Previous message: billthecat: "sed scripting"
- In reply to: Arvinder Singh: "replacing substring starting with a % sign"
- Next in thread: Icarus Sparry: "Re: replacing substring starting with a % sign"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 6 Nov 2003 01:38:46 +0000
In article <3b87395.0311051520.18702dc1@posting.google.com>,
Arvinder Singh wrote:
>I am trying to replace a substring "%n" with another value in a bash
>script.
>
>com=`echo ${comm//%n/ls}` doesnt seem to work. I tried escaping the %
>sign with a \ but that doesnt seem to work. I am new to this so any
>help would be appreciated.
You don't need the command substitution/echo since ${...//./} is
built-in to bash.
A '%' or '#' at the start of the pattern is special and makes the
rest of the pattern match the end or the start of the string respectively.
You might expect that quoting that character would protect it from
being interpretted (as '*' and '?' in the pattern behave this way)
however bash doesn't allow that. You could get around that problem
using a bracket expression;
comm=${comm//[%]n/ls}
The more portable way which works in any shell would be to use 'sed'
then you do need the command substitution;
comm=`echo "$comm" |sed 's/%n/ls/g'`
But this method has a few quirks because if $comm is "-n" or "-e"
it could be interpretted as an option by 'echo', if $comm contains
any backslash characters they may be interpretted by 'echo' and
if $comm has any trailing newlines they get deleted by the command
substitution itself. Most of the time those issues are not a problem
because you will know $comm can't contain such data thou' :)
byefornow
laura
-- echo alru_aafriehdab@ittnreen.tocm |sed 's/\(.\)\(.\)/\2\1/g'
- Next message: laura fairhead: "Re: deleting file with special character in name"
- Previous message: billthecat: "sed scripting"
- In reply to: Arvinder Singh: "replacing substring starting with a % sign"
- Next in thread: Icarus Sparry: "Re: replacing substring starting with a % sign"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|