The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
String Replacement Script jbud Shell Programming and Scripting 6 11-15-2007 06:37 AM
String Replacement with Perl Lindarella Shell Programming and Scripting 4 09-29-2006 03:05 PM
Adding a sequence string to a file MrPeabody Shell Programming and Scripting 5 07-21-2006 04:50 PM
sed problem - replacement string should be same length as matching string. amangeles Shell Programming and Scripting 4 01-11-2006 06:11 AM
String replacement in multiple files WABonnett Shell Programming and Scripting 2 02-17-2004 03:49 PM

Closed Thread
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 06-25-2008
raoscb raoscb is offline
Registered User
  
 

Join Date: May 2008
Posts: 16
Cool string replacement in a sequence of characters

Hi All,
I have a string "TBM630300000000020080506094041000003818".I want to replace the last nine digits with another string stored in a variabe called "count".The variabe is also having nine digits.Could any one please help me on this how to accomplish.I need a detail syntax(not in the short form) since i am new to the unix scripting.

I tried with some flavour of "string" commands but those are not supported by my KSH environment.

Appreciate your help!!!!!!!!!!!!!!!

Thanks
Narasimharao.
  #2 (permalink)  
Old 06-25-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Quote:
Originally Posted by raoscb View Post
Hi All,
I have a string "TBM630300000000020080506094041000003818".I want to replace the last nine digits with another string stored in a variabe called "count".The variabe is also having nine digits.Could any one please help me on this how to accomplish.I need a detail syntax(not in the short form) since i am new to the unix scripting.

I tried with some flavour of "string" commands but those are not supported by my KSH environment.

Appreciate your help!!!!!!!!!!!!!!!

Thanks
Narasimharao.
Here is something to start with


Code:
echo "your_string" | sed -e "s/\(.*\)........./\1$count/"

A . represents any character. A .* means any number of characters. \(.*\) would collect the characters into a buffer. Since we need to replace the last nine numbers, you can represent them as nine consecutive dots or as [0-9]\{9\}. This will get replaced with the contents of $count.
  #3 (permalink)  
Old 06-25-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,938
Using ksh93

Code:
$ str="TBM630300000000020080506094041000003818"
$ print ${str%{9}([[:digit:]])}
TBM630300000000020080506094041
$ count="987654321"
$ print ${str%{9}([[:digit:]])}$count
TBM630300000000020080506094041987654321
$

  #4 (permalink)  
Old 07-08-2008
raoscb raoscb is offline
Registered User
  
 

Join Date: May 2008
Posts: 16
string replace ment

Hi vino,
First of all Thanks for you.Its working fine for my requirement.Still i need a little bit clarification on the below code.What is the 1 before the "$count" variable signifies? Please explain me.with out 1 its not working fine.

Thanks in Advance.

Narasimharao.





Quote:
Originally Posted by vino View Post
Here is something to start with


Code:
echo "your_string" | sed -e "s/\(.*\)........./\1$count/"

A . represents any character. A .* means any number of characters. \(.*\) would collect the characters into a buffer. Since we need to replace the last nine numbers, you can represent them as nine consecutive dots or as [0-9]\{9\}. This will get replaced with the contents of $count.
Closed Thread

Bookmarks

Tags
unix commands

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 03:04 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