string replacement in a sequence of characters


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers string replacement in a sequence of characters
# 1  
Old 06-25-2008
Tools 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  
Old 06-25-2008
Quote:
Originally Posted by raoscb
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  
Old 06-25-2008
Using ksh93
Code:
$ str="TBM630300000000020080506094041000003818"
$ print ${str%{9}([[:digit:]])}
TBM630300000000020080506094041
$ count="987654321"
$ print ${str%{9}([[:digit:]])}$count
TBM630300000000020080506094041987654321
$

# 4  
Old 07-08-2008
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
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Outputting characters after a given string and reporting the characters in the row below --sed

I have this fastq file: @M04961:22:000000000-B5VGJ:1:1101:9280:7106 1:N:0:86 GGGGGGGGGGGGCATGAAAACATACAAACCGTCTTTCCAGAAATTGTTCCAAGTATCGGCAACAGCTTTATCAATACCATGAAAAATATCAACCACACCA +test-1 GGGGGGGGGGGGGGGGGCCGGGGGFF,EDFFGEDFG,@DGGCGGEGGG7DCGGGF68CGFFFGGGG@CGDGFFDFEFEFF:30CGAFFDFEFF8CAF;;8... (10 Replies)
Discussion started by: Xterra
10 Replies

2. Shell Programming and Scripting

String replacement

Hi, I have a text file where all records come in one line (single line file), each record starts with 'BUCH' and ends with '@&' and if data is not there we get space instead. between '@&' and next record there might be some spaces, now I want to remove those spaces between '@&' and 'BUCH'. ... (4 Replies)
Discussion started by: maks475
4 Replies

3. Shell Programming and Scripting

String replacement.

Dear Friends, I want to replace following line with given line. It should grep/search following string in a file (input.txt) M/M SRNO: 000M/6200-0362498 COSMETIC PRO MALE FEMALE Once found it should replace it to following string. T_DLHNNO: 000M/6200-0362498 ... (7 Replies)
Discussion started by: anushree.a
7 Replies

4. Shell Programming and Scripting

First number sequence from string

Hi, I have a string like: DBMS stats (Number Used | Percentage of total): 10 | 1.00% I have a sed command to extract numbers from this string: sed "s///g;s/^$/-1/;" Output: 10100 However what I want the sed command to return is only the first number(regardless of its size) i.e.... (3 Replies)
Discussion started by: mccartj5
3 Replies

5. Shell Programming and Scripting

String replacement

Hi All, I have below file which has data in below format. #$ | AB_100 | AB_300 ()| AB_4 @*(% | AB-789 i want o/p as below format. | AB_100 | AB_300 | AB_4 | AB-789 So here there is no standard format. How we can achieve the same in unix ? Regards, (3 Replies)
Discussion started by: gander_ss
3 Replies

6. Shell Programming and Scripting

Removing Escape Sequence Characters

Hi All, I have added the script command to user profile so that to record the on-screen data.But when i i checked the O/P i could see lot of escape sequence is there way to remove it. (2 Replies)
Discussion started by: cutechaps
2 Replies

7. Shell Programming and Scripting

String replacement

I have one string string1=user/password:IP_ADDR:Directory I need to replace string1 value like store into string2 string2=user password:IP_ADDR:Directory i.e replace "/" character by '<space>' character But i wouldn't use any file in the meantime. Please help me......................... (6 Replies)
Discussion started by: mnmonu
6 Replies

8. Shell Programming and Scripting

String replacement

Hi I am new to shell scripting but i manage to do some simple things. I am trying to replace a string in one file. I am using sed to replace but it is not permanently writing to the file, rather it is temporary. I want to know whether is there any another method to replace a string in a file... (7 Replies)
Discussion started by: reddybs
7 Replies

9. Shell Programming and Scripting

Replacement of string

Hi I have a text file which contains the following. AAA,BBB,CCC,DDD AAA,BBB,CCC,DDD AAA,BBB,CCC,DDD How can I replace all CCC with 888, with other contents inside the file remain unchange? Please advice Desired output: AAA,BBB,888,DDD AAA,BBB,888,DDD AAA,BBB,888,DDD (1 Reply)
Discussion started by: c0384
1 Replies

10. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies
Login or Register to Ask a Question