The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-20-2009
RSC1985 RSC1985 is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 25
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
  #2 (permalink)  
Old 07-20-2009
ryandegreat25 ryandegreat25 is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 236
try..

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

  #3 (permalink)  
Old 07-20-2009
RSC1985 RSC1985 is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 25
Quote:
Originally Posted by ryandegreat25 View Post
try..

Code:
-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?
  #4 (permalink)  
Old 07-20-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 553
You may want to consider Perl. String manipulation in Perl is amazingly flexible.

Quote:
Originally Posted by RSC1985 View Post
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 View Post
...
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
  #5 (permalink)  
Old 07-20-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
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:

Code:
find / -name ksh

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

Code:
substr=${LOGNAME:0:2}
echo "$substr"


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

Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0