You may want to consider Perl. String manipulation in Perl is amazingly flexible.
Quote:
Originally Posted by RSC1985
I have replace one particular substring in a string.I could get the substring value using awk command.
...
I need to replace this with a new value in the same string.
Please throw some light on this.
|
To perform inline conversion of the substring "defg" to "DEFG" in the string "abcdefg":
Code:
$
$ echo $x
abcdefg
$
$ echo $x | perl -ne 's/defg/DEFG/; print'
abcDEFG
$
Quote:
Originally Posted by RSC1985
...
More over the above solution only replaces the first occurance of the substring. But if i have many occurances like that how can i do that if want the replacement to be done at a particular place?
|
Let's say the string is "abcdefg abcdefg abcdefg". And you want to replace (inline) the second occurrence of "defg" to "UVWXYZ".
Code:
$
$ # Original string => "abcdefg abcdefg abcdefg"
$ # Transformed string => "abcdefg abcUVWXYZ abcdefg"
$
$ echo $y
abcdefg abcdefg abcdefg
$
$ echo $y | perl -ne '$c=0; s{(abc)(defg)}{if (++$c==2){ $1."UVWXYZ" }else{ $1.$2 }}gex; print'
abcdefg abcUVWXYZ abcdefg
$
HTH,
tyler_durden
---------- Post updated at 11:49 AM ---------- Previous update was at 11:40 AM ----------
The code posted above was the solution of the problem: "In the string S, replace inline the Nth occurrence of substring X to Y".
If the problem is: "In the string S, replace inline to X, the substring of length M starting at position N", then that is simpler in Perl.
Code:
$
$ echo $y
abcdefg abcdefg abcdefg
$
$ # Replace the substring of 4 characters, starting at position 11, to "UVWXYZ"
$
$ echo $y | perl -ne 'substr($_,11,4)="UVWXYZ"; print'
abcdefg abcUVWXYZ abcdefg
$
tyler_durden
|