vi/vim : complex pattern substitution


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers vi/vim : complex pattern substitution
# 1  
Old 08-03-2004
vi/vim : complex pattern 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  
Old 08-03-2004
for anybody with a similiar problem.. i received the issue on another forum:

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

# 3  
Old 08-03-2004
Although the solution is found, it's worth mentioning that you can also pipe a file through a command using {!} from within vi, e.g.
Code:
{!}awk -F'.' '{print "COUNT(DISTINCT(" $0 ")) as " $2 ","}'

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - pattern match - apply substitution

Greetings Experts, I am on AIX and in process of creating a re-startable script that connects to Oracle and executes the statements. The sample contents of the file1 is CREATE OR REPLACE VIEW DB_V.TAB1 AS SELECT * FROM DB_T.TAB1; .... CREATE OR REPLACE VIEW DB_V.TAB10 AS SELECT * FROM... (9 Replies)
Discussion started by: chill3chee
9 Replies

2. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

3. UNIX for Dummies Questions & Answers

Substitution mid pattern?

Hi there, I have a file that goes like this: b_cdbc_db_cd_bcd_aaa-bcd_cd That type of format, for many lines. What I want to do is enter a new line character for after the _ I write an expression to find "_...-" fine, but I don't know how to substitute this to be: "_\naaa-" - where... (1 Reply)
Discussion started by: maximus73
1 Replies

4. Shell Programming and Scripting

sed pattern substitution issue?

Hello everyone ... I'm going crazy, I hope some of you can help me ... I have to replace a line in a crontab like this: 5 2 * * 2 root backupdat with this: 5 5 * * 3 root backupdat the command I use is the following: sed -i.bak -e 's/5 2 * * 2 root backupdat/5 5 * * 3 root... (4 Replies)
Discussion started by: ionral
4 Replies

5. UNIX for Dummies Questions & Answers

Vim help - delete words in a file or characters after pattern

I have a file with words that begin with character #. Whenver that character is found that word should be deleted throughout the file. How do I do that in VIM. e.g: afkajfa ladfa ljafa #222222 kjafad ljl afajkj kjlj uouu #44444 jlkj lkjl Output should be afkajfa ladfa ljafa kjafad... (1 Reply)
Discussion started by: osbourneric
1 Replies

6. Solaris

Very Importan - Vim Settings - Error while opening a File using vim

I downloaded vim.7.2 and compiled the vim source . Added the vim binary path to PATH (Because iam not the root of the box) when i load the file using vim it throws me an error Error detected while processing /home2/e3003091/.vimrc: line 2: E185: Cannot find color scheme darkblue line... (0 Replies)
Discussion started by: girija
0 Replies

7. UNIX for Dummies Questions & Answers

Vim: help with substitution

What is the appropriate command should i use to insert a character(example A) in front of line 1 to line 200...Pls help (7 Replies)
Discussion started by: 793589
7 Replies

8. Shell Programming and Scripting

Perl:string substitution Pattern: ='abc...',

Hi friends, I want to substitute "a ='....'," with ":" in everywhere in a string using Perl. Details: ---------- my $str= " c1='fgfasfgasggfgff.,akhkhahha', c2='bbbn', c3='hg5 sh' "; Required o/p: $str= " c1:c2:c3 " I tried as below: $str=~ s/=\'.*\',/:/g ; print "str=... (14 Replies)
Discussion started by: Niroj
14 Replies

9. Shell Programming and Scripting

complex command substitution

hi, I have to execute this line below from within a shell script; simply backquoting it is not doing the trick; it is mangling up all the options; but when i type it out on a command line, it executes cleanly. Please help me in getting this right; $ vlc -I dummy --sout='#transcode{vcodec=mp4v,... (5 Replies)
Discussion started by: spopuri
5 Replies

10. Shell Programming and Scripting

pattern match and substitution, can you help?

pattern match and substitution, can you help? file named test.txt I want to replace all the words Event with the word Fatal in all lines containing the word ERR - but I also want to keep the output of the other lines not matching ERR Test.txt: Event 13 INF egegegege Event 14 INF... (4 Replies)
Discussion started by: frustrated1
4 Replies
Login or Register to Ask a Question