LinuxQuestions.org
Review your favorite Linux distribution.
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 10-14-2003, 10:39 PM   #1
king8
LQ Newbie
 
Registered: Sep 2003
Location: Burien, WA
Distribution: RH9
Posts: 12

Rep: Reputation: 0
Its the VIM challenge!


Ok guys,

If you wanted to change the following paragraph in VIM using only commands. How would you replace the uppercase letters of every word except for those starting a sentence to lowercase?


The vi editor has a command, tilde (~),
that changes lowercase letters to
uppercase and vice versa.
Unfortunately the ~ command does not
work with a Unit of Measure or
a Repeat Factor, so you have to change
the case one character at a time.

:-D and, no I don't know how, or else I wouldn't be asking

Good luck, and thank you

 
Old 10-15-2003, 05:31 AM   #2
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
I would move the curser over the character in question...then press [r] then type the replacement. Repeat for all characters that need to be changed. So in your case...place the curser over the "U" in "Unit of Measure", then press 'r' (literally: the 'r' key) then 'u' and presto chango, it's a lowercase 'u'. Repeat.
 
Old 10-15-2003, 08:00 AM   #3
arobinson74
Member
 
Registered: Nov 2002
Location: Lone Tree, CO
Distribution: Xubuntu Gutsy
Posts: 174

Rep: Reputation: 30
I haven't built it, but a look behind regular expression should be able to handle this. You just need to look for a character not after a period, and lower case it. The result would be something like this:

Normal Regex to match with a look behind:
/(?<=\.)(\w)/

VIM command to lowercase all uppercase letters
:%s/\([A-Z]\)/\l\1/g

Now, this will convert all upper-case to lower-case. You need to figure out now how to incorporate look behind matching in a VIM regex. Then, you can look for all uppercase letters not after a period and lower case them. Now, you have to also be careful of the first character and those on a new line. so you don't want to convert "^[A-Z]". This means you have to look at treating the block as new-lines as well.

As I said, this is a start, as it is a complex regex, so take this and see if you can finish it up.
 
Old 10-15-2003, 08:27 AM   #4
QuadMonk
LQ Newbie
 
Registered: Apr 2002
Location: Texas
Distribution: Slackware 8.0, RH 7.1
Posts: 6

Rep: Reputation: 0
You'll want to use something like sed or perl to do this.

This link will give you some useful input.

http://www.netsys.com/sunmgr/1997-10/msg00027.html
 
Old 10-15-2003, 08:28 AM   #5
QuadMonk
LQ Newbie
 
Registered: Apr 2002
Location: Texas
Distribution: Slackware 8.0, RH 7.1
Posts: 6

Rep: Reputation: 0
Talking

Looks like Andrew's got the better option.

Thanks.
 
Old 10-15-2003, 06:09 PM   #6
king8
LQ Newbie
 
Registered: Sep 2003
Location: Burien, WA
Distribution: RH9
Posts: 12

Original Poster
Rep: Reputation: 0
Lightbulb Good sugestions!

Thanks for your help guys, I'll try these out and let you know my results.

 
Old 03-24-2014, 12:44 PM   #7
Reefersleep
LQ Newbie
 
Registered: Mar 2014
Posts: 1

Rep: Reputation: Disabled
I'm trying to improve upon my Vim skills and was randomly googling for "vim regex challenge" when I came upon this thread, and decided to pick up the challenge.

I think I've come up with something that finds an uppercase alphabetical character that is not immediately preceded by the beginning of the file, the beginning of a line or a ., ! or ? followed by a space or tab and substitutes it with itself in lowercase.

If you accept every prompt, the following text:

The vi editor has a command, tilde (~),
that changes lowercase letters to
uppercase and vice versa.
Unfortunately the ~ command does not
work with a Unit of Measure or
a Repeat Factor, so you have to change
the case one character at a time.
Hello. I am Joe.
HOLA! I am José! Why are you looking at me weird? Is something WRONG?

becomes:

The vi editor has a command, tilde (~),
that changes lowercase letters to
uppercase and vice versa.
Unfortunately the ~ command does not
work with a unit of measure or
a repeat factor, so you have to change
the case one character at a time.
Hello. I am joe.
Hola! I am josé! Why are you looking at me weird? Is something wrong?


And it goes:

%s/\%^\@<!\_^\@<!\([.!?]\s\)\@<!\(\u\)/\l\2/gc


I'm sure it's very verbose - it could probably be shorter with magic or very magic. Possibly, the three negative lookbehinds could be reduced to one somehow, or substituted with something more elegant.

This is my first post on this ancient-by-internet-standards thread on a forum I've never seen before, but I just couldn't leave it hanging after constructing my own attempt
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
encryption with Vim (vim -x) mikshaw Linux - Software 2 03-07-2007 09:00 AM
Want a challenge? TruckStuff Linux - Security 2 05-13-2005 01:39 AM
here lies a challenge!!!! jayboogie Linux - Newbie 7 12-20-2004 09:41 PM
a challenge for you derfaust Programming 3 03-12-2004 12:23 AM
Bash challenge!!! Nimoy Programming 16 11-16-2003 03:30 AM

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

All times are GMT -5. The time now is 11:25 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