AWK Utitlity

Hi All,

Greetings for the day.
I request your thoughts on the below.
I have replace one particular substring in a string.I could get the substring value using awk command.

echo | awk '{ print substr("'"${String}"'",3,4) }'.

I need to replace this with a new value in the same string.
Please throw some light on this.

Thanks,
RSC1985

try..

-bash-3.2$ echo $String
abcdefg
-bash-3.2$ echo ${String/cdefg/CDEFG}
abCDEFG
-bash-3.2$

Thanks for the prompt response. I'm using Ksh.Most of the string functions are not working.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?

If you have not veryveryold ksh then string manipulation is like posix says. (Posix has used ksh93 ...). So you can use bash, ksh93, ...
If you use some commercial unix, usually they have many ksh version in different location:

find / -name ksh

How to test have you ksh93/posix compatible shell ? Ex. take two first letters from your LOGNAME variable:

substr=${LOGNAME:0:2}
echo "$substr"
# take first two letters then something new next 4 letters and rest of str is still old
newstr="${str:0:2}NEW${str:6}"

You may want to consider Perl. String manipulation in Perl is amazingly flexible.

To perform inline conversion of the substring "defg" to "DEFG" in the string "abcdefg":

$
$ echo $x
abcdefg
$
$ echo $x | perl -ne 's/defg/DEFG/; print'
abcDEFG
$

Let's say the string is "abcdefg abcdefg abcdefg". And you want to replace (inline) the second occurrence of "defg" to "UVWXYZ".

$
$ # 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.

$
$ 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