UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Dell PowerEdge Servers
  #1  
Old August 3rd, 2004, 12:34 AM
c444l c444l is offline
contains a pressurised widget
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: NC USA
Posts: 401 c444l User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 39 m 41 sec
Reputation Power: 5
Send a message via AIM to c444l Send a message via Yahoo to c444l
vi/vim : complex substitution ?

I use vim. I have a lot of SQL queries to write, and am hoping there is some wild command I can use in vim to make this simpler.

From a file that is a list of fields, like the excerpt, for example:

Code:
orderdetail.ccntyfips
orderdetail.citemord
orderdetail.coffdetid


I want to go to this:

Code:
COUNT(DISTINCT(orderdetail.ccntyfips)) as ccntyfips,
COUNT(DISTINCT(orderdetail.citemord)) as citemord,
COUNT(DISTINCT(orderdetail.coffdetid)) as coffdetid,


I'm not sure if such a thing can be done with one line of command. Fortunately, all lines will be in the same format.. tablename.fieldname. so I know that I need to do something like:

Code:
:%s/[tablename].[fieldname]/COUNT(DISTINCT([tablename].[fieldname])) as [fieldname],/g


If this is possible what would the proper syntax be?

Reply With Quote
  #2  
Old August 3rd, 2004, 01:13 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,059 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 3 h 46 m 33 sec
Reputation Power: 8
%s/orderdetail\.\(.*\)/COUNT(DISTINCT(orderdetail.\1)) as \1,/g

'sed' is for this better, exactly the same sintax.

Reply With Quote
  #3  
Old August 3rd, 2004, 01:43 AM
c444l c444l is offline
contains a pressurised widget
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: NC USA
Posts: 401 c444l User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 39 m 41 sec
Reputation Power: 5
Send a message via AIM to c444l Send a message via Yahoo to c444l
works beautifully.. for future reference..

can you (or anyone, of course)
explain this bit :
Code:
\.\(.*\)

Reply With Quote
  #4  
Old August 3rd, 2004, 02:39 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,059 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 3 h 46 m 33 sec
Reputation Power: 8
\. the \ masks the dot, dot in vi, sed, awk, grep, perl....
is not a dot, but a metacharacter replacing every oder
char, so a masked dot is a dot clear?
\( means: attention here is starting something
\) closes \)
.* any char, see (dot) below, the * is for NULL or INFINITY
iteration of precedenting char
finally
\1 show all chars found between \( and \)

ge:
echo aaa bbb 222 333 | sed -e 's/\(.*\) \(.*\) \(.*\) /\4 \1 \3 \2/'
prints
333 aaa 222 bbb

learn regular expression, it's a MUST in *nix
nota: they are used in the same manner in vi, sed, awk, grep, perl

Reply With Quote
  #5  
Old August 3rd, 2004, 02:45 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,059 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 3 h 46 m 33 sec
Reputation Power: 8
excuse typos:

\) closes \(
...
s/\(.*\) \(.*\) \(.*\) /
should be
s/\(.*\) \(.*\) \(.*\) \(.*\)/

Reply With Quote
  #6  
Old August 3rd, 2004, 03:04 AM
c444l c444l is offline
contains a pressurised widget
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: NC USA
Posts: 401 c444l User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 39 m 41 sec
Reputation Power: 5
Send a message via AIM to c444l Send a message via Yahoo to c444l
okay.. great thanks. I figured out most of it, but I was not sure why you were escaping the first dot.. now I know.

Some of the regular expression stuff I know, but just basic stuff. I love the power of vi and your posts have helped me with a lot of things I am going to do.. thank you!

I am planning on studying more on regular expressions, just have not gotten to that item on my list yet

Reply With Quote
  #7  
Old August 3rd, 2004, 03:08 AM
c444l c444l is offline
contains a pressurised widget
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: NC USA
Posts: 401 c444l User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 39 m 41 sec
Reputation Power: 5
Send a message via AIM to c444l Send a message via Yahoo to c444l
Oh, and I forgot to add that I had also changed the command to this :

Code:
%s/\(.*\)\.\(.*\),COUNT(DISTINCT(\1.\2)) as \2,/g

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > vi/vim : complex substitution ?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway