Thought of sharing some useful info which can help you to do your work faster in vi.You can use awk/sed scripts in vi using following command in vi
:%!scriptname
Here the scriptname file should have execute privilges for user. I used this to create a useful script which I was doing by typing multiple substitution command in vi.
e.g Your file contains list of table
$cat t.lst
BANK005
BJSTM
BJS_ORG
CHAINED_ROWS
CORR_BAM
CORR_CAM
CORR_EIT
CORR_GAC
CORR_GAM
CORR_ITC
CORR_LDT
CORR_LHT
Create script (quotes) with following command and give execute permission to user.
sed -e “s/^/’/g” -e “s/$/’,/” $1|awk ‘{printf (“%s”,$0)}’|sed -e “s/^/(/g” -e “s/,$/)/g”
open t.lst in vi and type :%!quotes
('BANK005','BJSTM','BJS_ORG','CHAINED_ROWS','CORR_BAM','CORR_CAM','CORR_EIT','CORR_GAC','CORR_GAM','CORR_ITC','CORR_LDT','CORR_LHT')
Similarly if you wish to remove blank lines, have a file blank like
awk ‘!NF==0 {print $0}’ $1
Blank lines can also be directly removed from vi using :g/^$/d
Isn’t it cool.. 🙂
Related
Tags: TipsUnixvi
Hi!
It’s a very useful feature to pipe contents of a vi buffer through some unix command. However, vi has powerful text editing commands that nearly equal sed. The command
:%s/.*/’&’/ | 1,$-1s/$/,/ | %j! | 1s/.*/(&)/
does about the same as our quotes script. You can register it as an ex command with
:command Quotes %s/.*/’&’/ | 1,$-1s/$/,/ | %j! | 1s/.*/(&)/
and execute it with
:Quotes
though this probably only makes sense if you store it in your local .vimrc.
Hi Chris,
Thanks ..It is very cool..I have not seen few of commands like %j!..I would say i really needed it..
Cheers
Amit
Command :%j! is very useful as it can be used to format the output given by V$SQLTEXT
When you use :%j! it will join line and remove space.