sed add special character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed add special character
# 1  
Old 08-31-2016
sed add special character

Hi all

I got test.test.test and need

test.test\.test *

I need the backslash before the last dot in the line

I tried

Code:
echo test.test.test | sed 's/\./\\./g'

but it gives me

test\.test\.test

Thanks
# 2  
Old 08-31-2016
Hello stinkefisch,

Following may help you in same.
Code:
echo "test.test.test" | sed 's/\(.*\.\)\(.*\)/\1\\\2 */g'

Output will be as follows.
Code:
test.test.\test *

In awk following may help you in same.
Code:
echo "test.test.test" | awk -F"." '{$NF="\\"$NF " *"} 1' OFS="."

EDIT: Adding one more solution for same now too.
Code:
cat script.ksh
A="test.test.test"
B=${A%.*}
C=${A##*.}
 echo $B".\\"$C" *"

So when we run above then following output will come.
Code:
 ./script.ksh
test.test.\test *

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-31-2016 at 07:23 AM.. Reason: Added one more solution on same now.
# 3  
Old 08-31-2016
I can't figure it out ?

---------- Post updated at 05:17 AM ---------- Previous update was at 05:14 AM ----------

Quote:
Originally Posted by RavinderSingh13
Hello stinkefisch,

Following may help you in same.
Code:
echo "test.test.test" | sed 's/\(.*\.\)\(.*\)/\1.\2/g'

Output will be as follows.
Code:
test.test..test

In awk following may help you in same.
Code:
echo "test.test.test" | awk -F"." '{$NF="."$NF} 1' OFS="."

EDIT: Adding one more solution for same now too.
Code:
cat script.ksh
A="test.test.test"
B=${A%.*}
C=${A##*.}
echo $B".."$C

So when we run above then following output will come.
Code:
 ./script.ksh
test.test..test

Thanks,
R. Singh
I need test.test\.test * as output
# 4  
Old 08-31-2016
Hello stinkefisch,

Apologies for same(logic was correct and still same now) but I have put . rather than \, so changed it in above post now.

Thanks,
R. Singh
# 5  
Old 08-31-2016
Many thanks works now Smilie
# 6  
Old 08-31-2016
Small adaption to your non-working solution:
Code:
echo test.test.test | sed 's/\.[^.]*$/\\& */'
test.test\.test *

With RavinderSingh13's variable A, try:
Code:
echo "${A%.*}\\.${A##*.} *"
test.test\.test *

# 7  
Old 08-31-2016
Consider also:
Code:
echo test.test.test|sed 's/\./\\&/2
s/$/ */'

producing the output:
Code:
test.test\.test *

The 2 (or any number n) flag (specifying the nth match of the BRE in the search pattern) in the substitute command being one of the few extensions that are available in sed above and beyond the standard ed and ex editing commands capabilities.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

With sed replaced special character is printed twice, why?

Dear all, I was wondering If you could help me out. I Am using a batch script to midfy some text files. Input: 912856 912857 912904Amongst others I use this line: REM I want to replace all lines that start with a 6-digit Number with a ftp command "get" followed by a path and the 6-digit... (9 Replies)
Discussion started by: SIEMI
9 Replies

2. Shell Programming and Scripting

sed command to add special character (')

I have a file (input) which contains are below. Member Analytics Engine Enterprise Manager Dev Tutorial I want to change contains as below by using sed command 'Member Analytics Engine'; 'Enterprise Manager'; 'Dev Tutorial'; First, I tried to add (') on every first line by using sed... (8 Replies)
Discussion started by: anshu ranjan
8 Replies

3. UNIX for Dummies Questions & Answers

Replacing special character with sed

Hi All, I have a text file that contains I1SP2 *=*=Y=M=D001D My requirement is to replace all occurrence of =* to =Z expected o/p is I1SP2 *=Z=Y=M=D001D I have tried with sed 's/=*/=Z/g' file sed 's!\=*!\=Z/g' file sed 's!\=*!\=Z!g' file sed 's!\=\*!\=Z!g' file but its not... (3 Replies)
Discussion started by: gotamp
3 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

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

6. 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

7. Shell Programming and Scripting

Decode %s Special Character in Sed

Greetings, I am doing something that I don't know if it is possible... I have a file with a line looks like this: <%s \n%s / %s \n%s \n> and I am trying to replace this line with <%s \n%s \n%s / %s \n%s \n> in Shell script with sed command... StringToReplace='%s \n%s / %s \n%s \n'... (2 Replies)
Discussion started by: wasabihowdi
2 Replies

8. Shell Programming and Scripting

Special Character SED/AWK removal

I have a script that produces an output containing '/.ssh'. I am trying to find a way of parsing only this data from a single line, without removing any other special characters contained within the output as a result of the parse. Any help would be appreciated (6 Replies)
Discussion started by: Raggedranger333
6 Replies

9. Shell Programming and Scripting

Sed-Special character replacement

Hi I want to replace ./testsed.ksh with testsed.ksh ./ is to be removed scriptnm=`sed -e 's/\.///' $0 does not work Please help (3 Replies)
Discussion started by: usshell
3 Replies

10. Shell Programming and Scripting

sed and special character in data

I have a script that is reading an existing report, pulling out the customer code, then tacking on the customer name from another file and replacing the existing customer code with the new field. This was written for me by someone else. I'm not real familiar with sed. The data is getting into... (3 Replies)
Discussion started by: MizzGail
3 Replies
Login or Register to Ask a Question