# SED Newb - Range Rage!

**URL:** <https://community.unix.com/t/sed-newb-range-rage/242005>\
**Category:** Shell Programming and Scripting\
**Created:** [June 8, 2009, 8:11am UTC](https://community.unix.com/t/sed-newb-range-rage/242005 "2009-06-08T08:11:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![GoneShootin](https://community.unix.com/user_avatar/community.unix.com/goneshootin/32/5142_2.png) [@GoneShootin](https://community.unix.com/u/GoneShootin)\
**Post date:** [June 8, 2009, 8:11am UTC](https://community.unix.com/t/sed-newb-range-rage/242005/1 "2009-06-08T08:11:07Z")

</div>

Hi all

First time poster long time lurker. I've been trying to get my head around SED but I think my beginner status is starting to prove too great a hindrence. I have nearly 100 CSS files that I need to modify in such a way that

```nohighlight
label, b, p .text{
some style stuff
}

```

would become

```nohighlight
b ,p .text{
some style stuff
}

label {custom style stuff}

```

label can appear above in the following ways

```nohighlight
label ,...

```

```nohighlight
label, ....

```

```nohighlight
, label...

```

```nohighlight
,label...

```

What I was trying to do was operate the the different flavours of 'label', and then find the next closing tag '}'. I'm testing the finding of 'label' with

```nohighlight
sed 's/,label//g;s/, label//g;;s/label,//g;s/label ,//' test.css

```

```nohighlight
test.css

body{

    color: #000000;
    font-family: Verdana, Arial, Helvetica, Sans-Serif;
    font-size: 10px;
    margin: 0px;
    padding: 0px;
        background:white;
        /*background-image: url(images/background.gif);*/
}

label ,b, p, .text  
{
    color: #000000;
    font-family: Verdana, Arial, Helvetica, Sans-Serif;
    font-size: 10px;
}

label,b, p, .text  
{
    color: #000000;
    font-family: Verdana, Arial, Helvetica, Sans-Serif;
    font-size: 10px;
}

b, p, label .text  
{
    color: #000000;
    font-family: Verdana, Arial, Helvetica, Sans-Serif;
    font-size: 10px;
}

b, p,label .text  
{
    color: #000000;
    font-family: Verdana, Arial, Helvetica, Sans-Serif;
    font-size: 10px;
}

```

If anyone could give me some direction I'd really appreciate it.

---

<div class="post-metadata">

**Author:** ![panyam](https://community.unix.com/letter_avatar/panyam/32/5_5575768a8748004e209b776fc1b2916d.png) [@panyam](https://community.unix.com/u/panyam)\
**Post date:** [June 8, 2009, 8:36am UTC](https://community.unix.com/t/sed-newb-range-rage/242005/2 "2009-06-08T08:36:47Z")

</div>

something like this ? :

```nohighlight
sed 's|\(.*\)\(label\)\(.*\)|\1\3|g' inputfile.txt

```

---

<div class="post-metadata">

**Author:** ![ahmad.diab](https://community.unix.com/user_avatar/community.unix.com/ahmad.diab/32/940_2.png) [@ahmad.diab](https://community.unix.com/u/ahmad.diab)\
**Post date:** [June 8, 2009, 8:40am UTC](https://community.unix.com/t/sed-newb-range-rage/242005/3 "2009-06-08T08:40:23Z")

</div>

kindly write the output you seek for....

---

<div class="post-metadata">

**Author:** ![GoneShootin](https://community.unix.com/user_avatar/community.unix.com/goneshootin/32/5142_2.png) [@GoneShootin](https://community.unix.com/u/GoneShootin)\
**Post date:** [June 8, 2009, 9:22am UTC](https://community.unix.com/t/sed-newb-range-rage/242005/4 "2009-06-08T09:22:54Z")

</div>

panyam that kind of works on the first step, which is neatly removing label. It does however still leave the comma's. Your code outputs (for example)

```nohighlight
,b, p, .text
{
        color: #000000;
        font-family: Verdana, Arial, Helvetica, Sans-Serif;
        font-size: 10px;
}

```

against the original

```nohighlight
label ,b, p, .text
{
        color: #000000;
        font-family: Verdana, Arial, Helvetica, Sans-Serif;
        font-size: 10px;
}

```

Note how in your output the trailing comma after label (,) still remains - which would of course give incorrect CSS code.

The 2nd part of this PAIN is that after above has ran it would need to be able to find the next closing tag ( } ) and append

```nohighlight
label {custom style stuff}

```

---

<div class="post-metadata">

**Author:** ![ahmad.diab](https://community.unix.com/user_avatar/community.unix.com/ahmad.diab/32/940_2.png) [@ahmad.diab](https://community.unix.com/u/ahmad.diab)\
**Post date:** [June 8, 2009, 10:23am UTC](https://community.unix.com/t/sed-newb-range-rage/242005/5 "2009-06-08T10:23:36Z")

</div>

use the below code it will work every time.

sed 's/\,\*[]_label[]\*\,_//g'

---

<div class="post-metadata">

**Author:** ![GoneShootin](https://community.unix.com/user_avatar/community.unix.com/goneshootin/32/5142_2.png) [@GoneShootin](https://community.unix.com/u/GoneShootin)\
**Post date:** [June 8, 2009, 10:31am UTC](https://community.unix.com/t/sed-newb-range-rage/242005/6 "2009-06-08T10:31:09Z")

</div>

> [@ahmad.diab](#):
>
> use the below code it will work every time.
> 
> sed 's/\,\*_label[]\*\,_//g'

Yes it does! Thank you ahmad. Now for the second part of my problem:

Based on the above SED command how can I find the next curly bracket "}" and append some text on the following line ?

---

<div class="post-metadata">

**Author:** ![ahmad.diab](https://community.unix.com/user_avatar/community.unix.com/ahmad.diab/32/940_2.png) [@ahmad.diab](https://community.unix.com/u/ahmad.diab)\
**Post date:** [June 8, 2009, 10:39am UTC](https://community.unix.com/t/sed-newb-range-rage/242005/7 "2009-06-08T10:39:01Z")

</div>

easy..

just write this literally :-

sed '  
/\}/ a\  
"write what ever you want"  
something...

' inputfile

note:- don't put any space after a\ .. just press ENTER key.

BR

---

<div class="post-metadata">

**Author:** ![GoneShootin](https://community.unix.com/user_avatar/community.unix.com/goneshootin/32/5142_2.png) [@GoneShootin](https://community.unix.com/u/GoneShootin)\
**Post date:** [June 8, 2009, 10:55am UTC](https://community.unix.com/t/sed-newb-range-rage/242005/8 "2009-06-08T10:55:37Z")

</div>

I dont think this will do what I need. Wont this append to all closing brackets? I just want to append to the next closing bracket after the LABEL SED has ran. Thanks for your help so far.

---

<div class="post-metadata">

**Author:** ![ahmad.diab](https://community.unix.com/user_avatar/community.unix.com/ahmad.diab/32/940_2.png) [@ahmad.diab](https://community.unix.com/u/ahmad.diab)\
**Post date:** [June 8, 2009, 11:13am UTC](https://community.unix.com/t/sed-newb-range-rage/242005/9 "2009-06-08T11:13:07Z")

</div>

You can put the line numbers ranges before appending the lines.

ex:  
sed '1,8 ....do somthing'

BR

---

<div class="post-metadata">

**Author:** ![GoneShootin](https://community.unix.com/user_avatar/community.unix.com/goneshootin/32/5142_2.png) [@GoneShootin](https://community.unix.com/u/GoneShootin)\
**Post date:** [June 8, 2009, 11:15am UTC](https://community.unix.com/t/sed-newb-range-rage/242005/10 "2009-06-08T11:15:55Z")

</div>

I could, but it wouldn't scale. There would be no gaurentee that the curly brace needle would be within the first 8 lines of the haystack.

Its a toughie!
