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 again with another problem with sed.

As the title says, I want sed to change lines starting at the 8th line to the end, but only lines which start with <b>.

I have code all ready and just wanted to append the argument to change lines starting with <b>.

Example of code sed -e '8,$ { s/\./\0<\/b>/ }' > "$file"_new

Example input:

<b><font color="#000000"><p align="JUSTIFY">COAT. how are you.
<b><font color="#000000"><p align="JUSTIFY">MONEY.Where are you.
<b><font color="#000000"><p align="JUSTIFY">FOOD.what are you.
<font color="#000000"><p align="JUSTIFY">FOOD.what are you.
<b><font color="#000000"><p align="JUSTIFY">CAR.which are you.
<font color="#000000"><p align="JUSTIFY">CAR.which are you.
<b><font color="#000000"><p align="JUSTIFY">QUALITY.When are you.

Example output:

<b><font color="#000000"><p align="JUSTIFY">COAT.</b>how are you.
<b><font color="#000000"><p align="JUSTIFY">MONEY.</b>Where are you.
<font color="#000000"><p align="JUSTIFY">FOOD.what are you.
<b><font color="#000000"><p align="JUSTIFY">FOOD.</b>what are you.
<b><font color="#000000"><p align="JUSTIFY">CAR.</b>which are you.
<font color="#000000"><p align="JUSTIFY">CAR.which are you.
<b><font color="#000000"><p align="JUSTIFY">QUALITY.</b>When are you.

These lines are situated from 8th line to end. I don't want any kind of change in 1-7th line.

share|improve this question
    
Is that example code not working? –  thefourtheye May 4 '13 at 13:17
    
Ya it working but all files are not as they suppose to be so i have to indicate the line starting with <b> also –  22lk94k943 only May 4 '13 at 16:22
    
What is that which you want to append? –  thefourtheye May 4 '13 at 17:02
    
the argument to change only the lines starting <b> –  22lk94k943 only May 4 '13 at 19:23
    
Is AU the best place for this question? –  blujay May 5 '13 at 5:22

1 Answer 1

up vote 2 down vote accepted

This should fit your requirements

cat TestSed.txt 

Line 1
Line 2
<b>Line 3.
Line 4
Line 5
<b>Line 6.
Line 7
Line 8
<b>Line 9.
Line 10.
<b>Line 11
<b>Line 12.

sed '8~1s/^<b>.*\.$/\0<\/b>/' < TestSed.txt

Line 1
Line 2
<b>Line 3.
Line 4
Line 5
<b>Line 6.
Line 7
Line 8
<b>Line 9.</b>
Line 10.
<b>Line 11
<b>Line 12.</b>

The sed here, does substitution s/Regular Expression To Search/Replacement Text/ and it applies this regual expression ^<b>.*\.$ over each and every line read and when it matches, it replaces it with \0<\/b>. Here \0 refers to the text which matched the regular expression ^<b>.*\.$.

^<b> will match lines starting with <b>

.* will match zero or more characters. So,

^<b>.* will match lines which are starting with <b> and has zero or more characters after that.

^<b>.*\.$ will match lines which are starting with <b> and has zero or more characters after that and ends with .

\0 in the replacement text will give the text which matched the regular expression. In our case, its the whole line.

\., \/ the backslashes are used here to escape . and /. They both have special meaning in regular expressions. We want to treat them as strings here. So we are escaping them.

8~1 tells sed to start applying the substitution from line number 8 and apply it after every 1 line, which means rest of the file. If you look at the example, it doesnt do any change on Line 3 and Line 6 though they match the regular expression.

EDIT:

sed '8~1s/^<b>[^\.]*\./\0<\/b>/' < TestSed.txt

This will match the text till the first .

[^\.] will match any character except .

[^\.]* will match any character except ., zero or more times.

Rest of the explanations are the same.

share|improve this answer
    
Thanks again for your helpful input, but as i have mentioned above in the examples that i want the </b> only after the first . and not any where else in the line. But the above comman is appending </b> after the last .. I also tried to remove $ after the . in the command i.e. sed '8~1s/^<b>.*\.$/\0<\/b>/' < TestSed.txt to sed '8~1s/^<b>.*\./\0<\/b>/' < TestSed.txt only to find out it does the same job. –  22lk94k943 only May 5 '13 at 6:39
    
And special thanks for the complete explanation of your command, it's such an valuable info for me :) –  22lk94k943 only May 5 '13 at 6:41
    
Thanks :) Updated the answer. –  thefourtheye May 5 '13 at 6:47
    
You did it again bro! :) Thanks a lot lot lot. –  22lk94k943 only May 5 '13 at 6:55
    
Thanks :) Even I learnt a lot about sed to answer your question. –  thefourtheye May 5 '13 at 6:56

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.