Replace prefix and suffix of a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace prefix and suffix of a string
# 1  
Old 01-14-2011
Replace prefix and suffix of a string

Hi, I'm new about shell scripting, and I need to do something like

abcd **1234** efgh

by

abcd '''1234''' efgh

I know that command sed helps about change one string by another, but I dont know how to keep whatever is inside **_** and replace * with '.

Thanks!
# 2  
Old 01-14-2011
Code:
echo "abcd **1234** efgh" | sed "s/*/'/g"

# 3  
Old 01-14-2011
Quote:
Originally Posted by anurag.singh
Code:
echo "abcd **1234** efgh" | sed "s/*/'/g"

I have tried this one but this change all the *, and I only want to change those strings who has two ** at the end and begginig. example:


**abcd **1234** efgh**
should be:
**abcd '''1234''' efgh**
# 4  
Old 01-14-2011
always post exact input (or most generalized one). Try following:
Code:
echo "**abcd **1234** efgh**" | sed "s/\*\*/'''/2;s/\*\*/'''/2"

OR
Code:
echo "**abcd **1234** efgh**" | sed "s/\*\*\([0-9]*\)\*\*/'''\1'''/"

This User Gave Thanks to anurag.singh For This Post:
# 5  
Old 01-14-2011
Code:
 # echo "**abcd **1234** **efgh**" | ruby -e 's=gets.chomp.split(/\s+/);s.each{|x|x.gsub!(/\*\*(.*?)\*\*/,"\47\\1\47")};END{puts s.join(" ")}'
**abcd '1234' 'efgh'

# 6  
Old 01-17-2011
[QUOTE=anurag.singh;302488022]always post exact input (or most generalized one). Try following:
Code:
echo "**abcd **1234** efgh**" | sed "s/\*\*/'''/2;s/\*\*/'''/2"


Thanks! That's was exactly what I want Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Prefix/Suffix on same file

Hi, I want to add prefix and suffix on line# 205 using SED or AWK and want to change on the same file without creating new file. This command will be used in the bash script Am using Bash shell Regards Nayaj (3 Replies)
Discussion started by: Nayaj
3 Replies

2. Shell Programming and Scripting

Removing only Prefix string (!)

Hello everyone, I want to remove only prefix ME_ from all the values that are present in the FILEA. Below code I'm using for this. sed 's/ME\_//g' FILEA > FILEB Using the above code, all ME_ values are getting removed from the file. But the problem here is I want to remove only Prefix ME_... (4 Replies)
Discussion started by: ed_9
4 Replies

3. Shell Programming and Scripting

AWK adding prefix/suffix to list of strings

75 103 131 133 138 183 197 221 232 234 248 256 286 342 368 389 463 499 524 538 (5 Replies)
Discussion started by: chrisjorg
5 Replies

4. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

5. Shell Programming and Scripting

QUERY_STRING with multiples appearances of the same string prefix

I have a shell script and it works pretty well to pull out the query_string contents of a single instance of router= But if I have multiple router= strings, it only pulls out the last one. Is there a way to pull out every router= and if there is a multple string, to egrep them together... (1 Reply)
Discussion started by: numele
1 Replies

6. UNIX for Dummies Questions & Answers

how to cut prefix from a string

I have a file: chromosome1:436728 chromosome2:32892 ..... chromosome22:23781 I just want to get the number, not the prefix "chromosomeX", so I want to remove all the prefix ahead of the numbers. How can I do that?? Thanks!!! (PS: give me some very simple command so that I can understand... (4 Replies)
Discussion started by: kaixinsjtu
4 Replies

7. Shell Programming and Scripting

prefix suffix to each argument

Hi, I have a variable, which contains comma separated values. Something like. StringA="abc,def,ghi,jkl" I want to apply prefix and suffix to each value in the string without using any loops. Say if Prefix is Pre_ and Suffix is _Suf then I need to get ... (1 Reply)
Discussion started by: tostay2003
1 Replies

8. Shell Programming and Scripting

Prefix a string to the contents of a file

Hi all, I have a requirement where in i have to create a temporary file by prefixing a string of special characters to each row of a input file. the input file is '|' delimited. here is the content of input file aaa|1234|axad|123 bbb|qwqw|qw|1334 the output should be ... (5 Replies)
Discussion started by: nvuradi
5 Replies

9. Shell Programming and Scripting

Need to replace a . with / which is having a matching Prefix

Hi Input File: export NAME='AA.BB.CC' export FILE=1.2.3 AA.BB.CC export MAIL= '1.3.3' export char='XX.YY.ZZ' Out File export NAME='AA/BB/CC' export FILE=1.2.3 AA.BB.CC export MAIL= '1.3.3' export char='XX/YY/ZZ' Only the Lines which have export and have alphabets after =... (9 Replies)
Discussion started by: pbsrinivas
9 Replies

10. Shell Programming and Scripting

How to cut prefix from a string?

Hi folks, I have the following parameter setting: export ADMIN_HOST_NAME=http://hostname.com I want to define a new parameter,ADMIN_HOST_NAME_NEW,which based on $ADMIN_HOST_NAME but I need to remove the prefix "http://". The requested result for $ADMIN_HOST_NAME_NEW is hostname.com How... (2 Replies)
Discussion started by: nir_s
2 Replies
Login or Register to Ask a Question