0

I have a file that contains :

<class>
these are special @ $ characters / < > & " '
</class>

I want to remove all the special characters only, between <class> </class>.

I tried this command.

sed  "s/[^a-zA-Z0-9(<.class>)(<\/class>)/ /g" file.txt

In above command I have tried to keep alphabets, numeral, tags and everything else should be removed.

But it's not giving me the output I want.

  • 1
    What is the input and what is the expected output. Also, is that the literal sed command that you tried? It is missing a ] somewhere. – Kusalananda Jan 28 '18 at 14:01
  • This seems like it might be an XY problem. What is the problem that you are trying to solve with this? There might be a better solution than sed, but I would need to know what your file currently looks like (is it just what you show here?) and what the file should look like. If you're trying to modify an HTML or XML file, sed isn't always the best tool for the job because it can produce a malformed document. – ErikF Jan 28 '18 at 14:15
  • Please look at How to do I ask a good question? – Hunter.S.Thompson Jan 30 '18 at 3:34
1

Assuming there is only one block of class and each tag is in a separate line, this will work for you in GNU awk:

awk '/<\/class>/{p=0};p{gsub(/[^A-Za-z0-9]/," ")};/<class>/{p=1};1' file.txt
| improve this answer | |
0

Done by below sed command tested and worked fine Using below command i removed all special characters [<>&$@/'"] between <class> and </class>

input.txt

<class>
these are special @ $ characters / < > & " '
</class>

command

sed -n '/<class>/,/<\/class>/p' input.txt | sed '/^[a-z]/s/[<>&$@/]//g' | sed "s/'//g" | sed 's/"//g'

output

<class>
these are special   characters
</class>
| improve this answer | |
  • 1
    that works as long as the <class> tags are on lines of their own – ilkkachu Jan 28 '18 at 15:06
  • 1
    This remove lines before <class> or after </class>. – Isaac Jan 28 '18 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.