Delete special characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete special characters
# 1  
Old 03-28-2018
Delete special characters

My sed is not working on deleting the entire special characters and leaving what is necessary.
Code:
grep connections_per a|sed -e  's/\<\!\-\-//g'

INPUT:
Code:
   <!-- <connections_per_instance>1</connections_per_instance> -->
 <method>HALF</method>
          <!-- <connections_per_host>1</connections_per_host> -->
          <dedicated_options_connection>true</dedicated_options_connection>
        </service>
        <service id="half">
          <method>FULL</method>
          <!-- <connections_per_host>1</connections_per_host> -->
          <dedicated_options_connection>true</dedicated_options_connection>

Output:
Code:
 <!-- <connections_per_instance>1</connections_per_instance> -->
 <method>HALF</method>
          <connections_per_host>5</connections_per_host>
          <dedicated_options_connection>true</dedicated_options_connection>
        </service>
        <service id="half">
          <method>FULL</method>
          <connections_per_host>5</connections_per_host>
          <dedicated_options_connection>true</dedicated_options_connection>


Last edited by rbatte1; 03-29-2018 at 06:28 AM..
# 2  
Old 03-28-2018
Code:
grep connections_per a|sed -e 's/<!--//g; s/-->//g;'

This does not change the value in the tag as shown in the output.
This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 03-28-2018
In addition to what rdrtx1 already explained:

If you want to change something only on certain lines and not on others you should do that in sed, not outside, with grep::

Code:
sed '/^special_line/ s/Y/X/g'

will change character "Y" to "X", but only on lines starting with "special_line". Likewise with your problem:

Code:
sed '/connections_per/ s/[<>]!*--//g' a

If you want only the changed lines as output:

Code:
sed -n '/connections_per/ s/[<>]!*--//g' a

As a rule of thumb: whenever you use sed, awk or a similar programmable text processing tool, do everything with it or use another tool. Any line with "...grep | sed | grep..." or "awk | sed | ..." is (very very rare cases aside) nonsense.

I hope this helps.

bakunin

Last edited by bakunin; 03-28-2018 at 05:18 PM.. Reason: typo
This User Gave Thanks to bakunin For This Post:
# 4  
Old 03-28-2018
Sir can you please copy the text, and test it. Also please check the output.
Thank you

---------- Post updated at 04:12 PM ---------- Previous update was at 04:08 PM ----------

Quote:
Originally Posted by rdrtx1
Code:
grep connections_per a|sed -e 's/<!--//g; s/-->//g;'

This does not change the value in the tag as shown in the output.
'''do you mind copy the input..

and test it?

and based on my output..hoping it is the same.
# 5  
Old 03-28-2018
Code:
sed '/<!--.*<connections_per_host>1</{s/<!--//; s/-->//; s/>.*</>5</};' INPUT

This User Gave Thanks to rdrtx1 For This Post:
# 6  
Old 03-28-2018
Thank you rdxt1, can you please explain, character per character please. the sed command.
Thank you
# 7  
Old 03-29-2018
Code:
sed
'            enclose First parameter / Script
/.../        for lines only matching ... pattern
{            start compound statement
s/.../.../   substitute pattern(s)
;            separate Statements
}            end compound Statement
'            end parameter

These 2 Users Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need help to delete special characters exists only at the end of the each record in UNIX file?

Hi, I have a file in unix with 15 columns.It consists special characters(#,$,^M,@,*,% etc)at the end of the each record.I want to remove these special characters.I used the following: Sed -e 's/ /g;s/ */ /g' . But It is removing special characters exists everywhere in the file(begining,middle... (24 Replies)
Discussion started by: rakeshp
24 Replies

2. Shell Programming and Scripting

Look for substrings with special characters

Hello gurus, I have a lookup table cat tmp1 \\\erw``~ 1 ^774574574565665f\] 2 ()42543^ and I`m trying to compare a bunch of strings such that, either the lookup table column 1, or the string to be looked up are substrings of each other (and return the second lookup column if yes). ... (2 Replies)
Discussion started by: sheetalk
2 Replies

3. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

4. Shell Programming and Scripting

special characters

Hey guys, I'm trying to replace "]Facebook" from the text but sed 's/]Facebook/Johan/g' is not working could you please help me with that? (6 Replies)
Discussion started by: Johanni
6 Replies

5. UNIX for Dummies Questions & Answers

How to see special characters?

Hi all, I was wondering how can i see the special characters like \t, \n or anything else in a file by using Nano or any other linux command like less, more etc (6 Replies)
Discussion started by: gvj
6 Replies

6. Shell Programming and Scripting

how to delete special characters from the file content

Hello Team, Any one suggest how to delte the below special character from a file which is having one column 10 rows of same below content. ---------------------------------------- Kosten|bersicht gemd_ ' =Welche Kosten kvnnen... (2 Replies)
Discussion started by: kanakaraju
2 Replies

7. Shell Programming and Scripting

Special characters

When I open a file in vi, I see the following characters: \302\240 Can someone explain what these characters mean. Is it ASCII format? I need to trim those characters from a file. I am doing the following: tr -d '\302\240' ---------- Post updated at 08:35 PM ---------- Previous... (1 Reply)
Discussion started by: sid1982
1 Replies

8. Shell Programming and Scripting

sed delete pattern with special characters

Hi all, I have the following lines <b>A gtwrhwrthwr text hghthwrhtwrtw </b><font color='#06C'>; text text (text) <b>B gtwrhwrthwr text hghthwrhtwrtw </b><font color='#06C'>; text text (text) <b>J gtwrhwrthwr text hghthwrhtwrtw </b><font color='#06C'>; text text (text) and I would like to... (5 Replies)
Discussion started by: stinkefisch
5 Replies

9. UNIX for Dummies Questions & Answers

How to delete a file with special characters

I don't now exactly how I did it, but I created a file named " -C " cexdi:/home1 $ls -lt total 1801336 -rw------- 1 cexdi ced-group 922275840 23 mars 10:03 -C How do I delete this file ? cexdi:/home1 $rm -C rm: invalid option -- C Syntax : rm filename ... Doesn't work...... (5 Replies)
Discussion started by: yveslagace
5 Replies

10. UNIX for Dummies Questions & Answers

special characters

I have one file which is named ^? ( the DEL character ) I'd like to know how to rename or copy the file by using its i-node number TYIA (2 Replies)
Discussion started by: nawnaw
2 Replies
Login or Register to Ask a Question