LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-07-2005, 08:46 AM   #1
walterbyrd
Member
 
Registered: Apr 2004
Posts: 734

Rep: Reputation: 46
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.
 
Old 12-07-2005, 09:39 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Would you settle for sed?
Code:
sed 's/^\([[:alpha:]].*\)/<b>\1<\/b>/' file.txt
 
Old 12-07-2005, 09:41 AM   #3
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
You can do something like this:
Quote:
:1,$s?^[A-Za-z].*?<b>&</b>?
 
Old 12-07-2005, 10:53 AM   #4
walterbyrd
Member
 
Registered: Apr 2004
Posts: 734

Original Poster
Rep: Reputation: 46
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.
 
Old 12-07-2005, 11:12 AM   #5
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
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.
 
Old 12-07-2005, 11:55 AM   #6
walterbyrd
Member
 
Registered: Apr 2004
Posts: 734

Original Poster
Rep: Reputation: 46
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 - ?
 
Old 12-07-2005, 12:17 PM   #7
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration