How to replace with a special character in String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace with a special character in String
# 1  
Old 08-23-2012
How to replace with a special character in String

Hi,

I am beginner to Shell Scripting.

I have a String like this "testabcdef", i need the first character as it is and the remaining character should be replaced by the the '*' character. e.g(t***********)

PLZ Suggest me.
# 2  
Old 08-23-2012
one way

Code:
 
$ echo "abcd234234234" | nawk -v FS="" '{a=substr($0,1,1);for(i=2;i<=length($0);i++)a=a"*"; print a}'
a************

# 3  
Old 08-23-2012
Slightly different, without a loop:
Code:
$ echo "abcd234234234"| awk '{c=$1; gsub(/[a-z0-9]/,"*"); $1=c; print; }' FS= OFS=
a************

This User Gave Thanks to zaxxon For This Post:
# 4  
Old 08-23-2012
Quote:
Originally Posted by zaxxon
Slightly different, without a loop:
Code:
$ echo "abcd234234234"| awk '{c=$1; gsub(/[a-z0-9]/,"*"); $1=c; print; }' FS= OFS=
a************

Thank You for your Replay,

It's Working fine, but i am not able to store the result in a variable.
# 5  
Old 08-23-2012
Quote:
Originally Posted by nanthagopal
Thank You for your Replay,

It's Working fine, but i am not able to store the result in a variable.

Use this..

Code:
$ var=$(echo "abcd234234234"| awk '{c=$1; gsub(/[a-z0-9]/,"*"); $1=c; print; }' FS= OFS=)
$ echo $var
a************

# 6  
Old 08-23-2012
Only tested with bash (should work with ksh93):
Code:
x=abc; y=${x:1}; z=${x:0:1}${y//?/*}

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace special characters with backslash and character

Hi, I have a string wherein i need to replace special characters with backslash and that character. Ex: If my string is a=qwerty123@!, then the new string should be a_new=qwerty123\@\!\, Thanks (3 Replies)
Discussion started by: temp_user
3 Replies

2. Shell Programming and Scripting

Replace Special Character With Next Present Byte

Hi, First find the special character, from the special character take next two bytes convert the bytes to decimal and replace with next present byte of decimal value times. E.g. Input: 302619ú1A? Output: 302619(3 spaces for ú1A)?????????????????????????? Thanks, Dines (27 Replies)
Discussion started by: dineshnak
27 Replies

3. Shell Programming and Scripting

[Solved] Find and replace till nth occurence of a special character

Hi, I have a requirement to search for a pattern in each line in a file and remove the in between words till the 3rd occurrence of double quote ("). Ex: CREATE TABLE "SCHEMANAME"."AMS_LTV_STATUS" (Note: "SCHEMANAME" may changes for different schemas. Its not a fixed value) I need to... (2 Replies)
Discussion started by: satyaatcgi
2 Replies

4. Shell Programming and Scripting

How to replace special character using sed?

How can I replace the follong text including to number 7000? cat tmp0.txt Winston (UK) Wong I would the 7000 to replace Winston (UK) Wong. I fail with method below: sed ' s /Winston\(UK\)Wong/7000 tmp0.txt' (1 Reply)
Discussion started by: vivien_chu
1 Replies

5. Shell Programming and Scripting

replace /n with special character

I would like to replace /n with ',' and after replace remove last semicolon then put a open brace in starting and closing brace in end of line. See below example: input: 1234 3455 24334 234 output: ('1234,'3455',24334','234') Thanks (3 Replies)
Discussion started by: anupdas
3 Replies

6. Shell Programming and Scripting

how to replace the special character with another using SED

I have the replace the pattern in the file , ); to ); Could someone please help me to get this command. (2 Replies)
Discussion started by: mohan.bit
2 Replies

7. Shell Programming and Scripting

sed special character replace

I need to do the following: text in the format of: ADDRESS=abcd123:1111 - abcd123:1111 is different on every system. replace with: ADDRESS=localhost:2222 sed 's/ADDRESS=<What do I use here?>/ADDRESS=localhost:2222/g' Everything I've tried ends up with: ... (3 Replies)
Discussion started by: toor13
3 Replies

8. Shell Programming and Scripting

Remove box like special character from end of string

Hi All, How to remove a box like special character which appears at the end of a string/line/record. I have no clue what this box like special character is. It is transparent square like box. This appears in a .DAT file at the end of header. I'm to compare a value in header with a parameter.... (16 Replies)
Discussion started by: Qwerty123
16 Replies

9. Shell Programming and Scripting

Need help to extract a string delimited by any special character

I have a string as follows IS*blahblah TED~blahblah etc. I want to list down only IS and TED Can someone help me? (24 Replies)
Discussion started by: kumariak
24 Replies

10. Shell Programming and Scripting

replacing string with special character ???

the problem is while replacing the old string with new one with the help of SED i am unable to replace the special characters with new strings. how can i do that? i dont want the user to be given the trouble to write '\' before every special characters like * , . , \ , $ , &. sed... (4 Replies)
Discussion started by: imppayel
4 Replies
Login or Register to Ask a Question