Delete special characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete special characters
# 8  
Old 03-29-2018
I have been looking at the file structure:
1) All lines start with the " ", single space, character as an absolute minimum, are these indents and need to be preserved?
2) If so then line 1 is shifted 2 out of 3 " "'s, 2 spaces, leftwards on the output which implies "NO" to number 1)
3) If indentation IS required then you need to remove <!-- <- note the TRAILING space.
4) Similarly the other end --> <- again note the LEADING space.
5) And leave the 2 lost spaces intact in the first output line.
Just an observation that caught my attention...

Last edited by wisecracker; 03-29-2018 at 06:42 AM.. Reason: Add number 5)
This User Gave Thanks to wisecracker For This Post:
# 9  
Old 03-29-2018
Your explanation is awesome,...I understand now, what makes me confuse before was, I remember to do a backlash to any special character, But in the command below, theres none.

Thanks

Code:
 sed -i '/<!--.*<connections_per_host>1</{s/<!--//; s/-->//; s/>.*</>5</};' a

---------- Post updated at 08:52 AM ---------- Previous update was at 08:51 AM ----------

Quote:
Originally Posted by RudiC
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

I like your explanation,. i understand the syantax because of you. But pelase educate me, when to use a backslash to negate a special character?
Thanks
# 10  
Old 03-29-2018
Quote:
Originally Posted by kenshinhimura
But pelase educate me, when to use a backslash to negate a special character?
The rule is: when you use a character that has a special meaning (to sed) and you want it to mean just the character itself you need to "escape" it - that is, prepend it with a backslash.

An example:

Code:
sed 's/abc./xyz/' /input/file

The dot (".") here doesn't mean a dot, but is a special character, meaning "any one character". If you want it to mean a real, literal dot and nothing else, you need to escape it:

Code:
sed 's/abc\./xyz/' /input/file

Notice that this escaping is implicit in some situations, for instance in "character-classes". Look at the following regular expression:

Code:
sed 's/a[bcd]e/xyz/' /input/file

This searches for an "a", followed by either an "b", a "c" or a "d", followed by an "e". It would match any of these strings:

abe
ace
ade

Inside the brackets all characters lose their special meaning. I.e. a[bc.]d would search for any of these strings:

abd
acd
a.d

If you would try to escape a character here the escaping backslash would be treated as a normal character too. The regexp a[bc\.]d would find these strings:

abd
acd
a\d
a.d

Notice that the characters you called special - "<", ">", etc. - don't have any special meaning in sed anyway and would have been safe to use without any escaping in first place.

I hope this helps.

bakunin
# 11  
Old 03-29-2018
backslashes don't negate characters, they "escape" them, i.e. remove their special meaning, force them back to normal chars. Negate, BTW, is a difficult term with characters. You might mean "complement"?
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