LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How do I do this using vi? (https://www.linuxquestions.org/questions/linux-software-2/how-do-i-do-this-using-vi-390147/)

walterbyrd 12-07-2005 08:46 AM

How do I do this using vi?
 
I am editing an html file.

I want every line that starts at the leftmost column, with any alpha character, to be bold.

I want to go from:

Example Line

to

<b>Example Line</b>


I would rather do this with a few simple simple commands, as opposed to one very complex command.

homey 12-07-2005 09:39 AM

Would you settle for sed?
Code:

sed 's/^\([[:alpha:]].*\)/<b>\1<\/b>/' file.txt

Berhanie 12-07-2005 09:41 AM

You can do something like this:
Quote:

:1,$s?^[A-Za-z].*?<b>&</b>?

walterbyrd 12-07-2005 10:53 AM

Thanks, it worked great.

Would you please explain this to me a little?

:1,$s?^[A-Za-z].*?<b>&</b>?


I've always done a global substitute like: :%s/oldstring/newstring/g. You seem to use a $ instead of %, and ? instead of /. I'm sure there must be some good reason.

Why is there a "1" at the beginning?

Could I use [a-Z] instead of [A-Za-z] ?

I suppose ^[A-Za-z] means any char at the beginning of the line, and the * means anything that follows. But I don't understand the dot.

Berhanie 12-07-2005 11:12 AM

Quote:

I've always done a global substitute like: :%s/oldstring/newstring/g. You seem to use a $ instead of %, and ? instead of /. I'm sure there must be some good reason.
1,$s is the same as %s. The "1,$" just says explicitly that the substitution should be made on lines 1 to $ (last line). It's quicker just to use %.
For the separator character, you can use almost anything, not just /. I used ?. If the substitution involves a / (as in </b>), it's easier to use something other than a / so you don't have to escape it.[/quote]

Quote:

Could I use [a-Z] instead of [A-Za-z] ?
Bad idea, since you don't know what the order is. If it's ASCII, then all the caps occur before the smalls. [a-zA-Z] or [A-Za-z] is safe.

Quote:

I suppose ^[A-Za-z] means any char at the beginning of the line, and the * means anything that follows. But I don't understand the dot.
dot means any character except newline. * means 0 or more. So, .* means "everything up to the end of the line".

By the way, the ampersand (&) means "the thing that was matched". Also take a look at what homey did.

walterbyrd 12-07-2005 11:55 AM

Great, you are helping a lot. Can you answer just one more?

I have mistakes in the document like this: top<li>secret. I want to replace them with top-secret.

I can not do this with:

:%s/[A-Za-z]<li>/&-/g

or

:%s/[A-Za-z]<li>/[A-Za-z]-/g

so how do I replace the <li> with a - ?

Berhanie 12-07-2005 12:17 PM

If your string is always "top-secret", then you can do something simple like:
Quote:

:%s/top<li>secret/top-secret/g
Otherwise, if it's variable, like "x-y", you can use:
Quote:

:%s/\([A-Za-z]\+\)<li>\([A-Za-z]\+\)/\1-\2/g


All times are GMT -5. The time now is 05:21 PM.