|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
|
#2
|
|||
|
|||
|
%s/orderdetail\.\(.*\)/COUNT(DISTINCT(orderdetail.\1)) as \1,/g
'sed' is for this better, exactly the same sintax. |
|
#3
|
|||
|
|||
|
works beautifully.. for future reference..
can you (or anyone, of course) explain this bit : Code:
\.\(.*\) |
|
#4
|
|||
|
|||
|
\. 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 ![]() |
|
#5
|
|||
|
|||
|
excuse typos:
\) closes \( ... s/\(.*\) \(.*\) \(.*\) / should be s/\(.*\) \(.*\) \(.*\) \(.*\)/ |
|
#6
|
|||
|
|||
|
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 ![]() |
|
#7
|
|||
|
|||
|
Oh, and I forgot to add that I had also changed the command to this :
Code:
%s/\(.*\)\.\(.*\),COUNT(DISTINCT(\1.\2)) as \2,/g |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > vi/vim : complex substitution ? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|