Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

here is a sample for my text file:

<w:r><w:t>
<w:r w:rsidR="00D171FD">
<w:t></w:t>
</w:r><w:r>
<w:t xml:space="preserve">
This is a sample text </w:t>
</w:r>
<w:highlight w:val="green"/>
<w:r w:rsidR="00D171FD">
<w:color w:val="FF0000"/>
<w:t>
Sample text</w:t>
</w:r>

The problem is that I need both the pure text and the following tags only:
color w:val="FF0000"
highlight w:val="green"

How can this be done?

share|improve this question
    
Could you post your expected output from sed or awk? You might be better of with a xml parser for this, see e.g. unix.stackexchange.com/a/64708/8677 –  N.N. Mar 8 '13 at 16:34
    
I think I'm gonna make a script to do it, Thanks. –  Mina Mohsen Mar 8 '13 at 17:06
add comment

1 Answer 1

With the above command line it will function if you will never have those expression as text:

  1. <w:
  2. \>

The command line is:

cat Myfile.txt  | grep -E "color w:val=|highlight w:val=" | sed s/"<w:"/""/g | sed s/"\/>"/""/g

Explanation:

  • grep -E, --extended-regexp PATTERN
    Interpret PATTERN as an extended regular expression
  • | logical OR inside the PATTERN of grep
  • | pipe symbol in shell environment
  • sed s/"<w:"/""/g substitutes globally (everywhere) "<w:" with empty string ""

Note: it's possible to write sed in many other way and in a more compact way. I think so is didactic and can be used in a more wide range of possibility when is needed to substitute an expression with another.

This is the output:

highlight w:val="green"  
color w:val="FF0000"  
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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