0

I have a directory with many HTML documents. Most of them contain the codeblock

      .org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
        text-decoration: underline;
      }

inside the <style type="text/css"> tag. I'd like to write a script that removes the line text-decoration: underline; from this block in every file.

Usually I would write a sed or perl one-liner to simply delete each instance of text-decoration: underline; but many of the documents have other instances of the line text-decoration: underline that I do not removed.

Is there a tool in linux that can easily do this?

2

Try this:

sed '/.org-link {/,/}/{/text-decoration: underline;/d}' file

Output:

      .org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
      }

To edit your file "in place":

sed -i '/.org-link {/,/}/{/text-decoration: underline;/d}' file
  • I'm aware that sed or perl can do this. The problem is that there are other instances of text-decoration: underline; in the files that I don't want deleted. Maybe I should make this clear in my question. – Brian Fitzpatrick Aug 24 '15 at 6:01
  • Is there always a line with ".org-link {" to mark this block? – Cyrus Aug 24 '15 at 6:02
  • Yes. I want to delete the the first instance of font-weight: bold; after .org-link { – Brian Fitzpatrick Aug 24 '15 at 6:05
  • I've updated my answer. – Cyrus Aug 24 '15 at 6:06
  • Out of curiosity, how could I adjust the code if I wanted to replace this instance of text-decoration: underline with text-decoration: bold? – Brian Fitzpatrick Aug 24 '15 at 6:10
0

Using gawk:

gawk -i inplace '/.org-link {/,/}/ {if($0~/text-decoration: underline/) next} {print}' infile

This will delete only text-decoration: underline attributes in a .org-link class:

user@debian ~ % cat infile
.org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
        text-decoration: underline;
      }

.org-link1 {
        /* org-link */
        color: #b58900;
        font-weight: bold;
        text-decoration: underline;
      }

.org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
        text-decoration: underline;
      }
user@debian ~ % gawk -i inplace '/.org-link {/,/}/ {if($0~/text-decoration: underline/) next} {print}' infile
user@debian ~ % cat infile
.org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
      }

.org-link1 {
        /* org-link */
        color: #b58900;
        font-weight: bold;
        text-decoration: underline;
      }

.org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
      }

To loop this over multiple HTML files in the same working directory, you can use a bash for loop with wildcards:

for f in *.html; do gawk -i inplace '/.org-link {/,/}/ {if($0~/text-decoration: underline/) next} {print}' "$f"; done

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.