Use awk/sed in vi

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.. 🙂

3 thoughts on “Use awk/sed in vi

  1. 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.

  2. Command :%j! is very useful as it can be used to format the output given by V$SQLTEXT

     select sql_text from V$SQLTEXT where sql_id='avv9twxrav4gk' order by piece
    
    SQL_TEXT
    ----------------------------------------------------------------
    select decode(bitand(a.flags, 16384), 0, a.next_run_date,
             a.last_enabled_time),         a.obj#, decode(bitand(a.f
    lags, 16384), 0, 0, 1)  from sys.scheduler$_job a  where bitand(
    a.job_status, 3) = 1    and ((bitand(a.flags, 134217728 + 268435
    456) = 0) or         (bitand(a.job_status, 1024)  0))    and b
    itand(a.flags, 4096) = 0    and (a.class_oid is null      or (a.
    class_oid is not null      and a.class_oid in (select b.obj# fro
    m sys.scheduler$_class b                          where b.affini
    ty is null)))    and decode(bitand(a.flags, 16384), 0, a.next_ru
    n_date,               a.last_enabled_time) =  (select min(decode
    (bitand(c.flags, 16384), 0, c.next_run_date,             c.last_
    enabled_time))   from sys.scheduler$_job c   where bitand(c.job_
    status, 3) = 1     and ((bitand(c.flags, 134217728 + 268435456)
    = 0) or          (bitand(c.job_status, 1024)  0))     and bita
    nd(c.flags, 4096) = 0     and (c.class_oid is null       or (c.c
    lass_oid is not null       and c.class_oid in (select d.obj# fro
    m sys.scheduler$_class d                           where d.affin
    ity is null))))
    

    When you use :%j! it will join line and remove space.

    select decode(bitand(a.flags, 16384), 0, a.next_run_date,         a.last_enabled_time),         a.obj#, decode(bitand(a.flags
    , 16384), 0, 0, 1)  from sys.scheduler$_job a  where bitand(a.job_status, 3) = 1    and ((bitand(a.flags, 134217728 + 2684354
    56) = 0) or         (bitand(a.job_status, 1024)  0))    and bitand(a.flags, 4096) = 0    and (a.class_oid is null      or (
    a.class_oid is not null      and a.class_oid in (select b.obj# from sys.scheduler$_class b                          where b.a
    ffinity is null)))    and decode(bitand(a.flags, 16384), 0, a.next_run_date,               a.last_enabled_time) =  (select mi
    n(decode(bitand(c.flags, 16384), 0, c.next_run_date,             c.last_enabled_time))   from sys.scheduler$_job c   where bi
    tand(c.job_status, 3) = 1     and ((bitand(c.flags, 134217728 + 268435456)= 0) or          (bitand(c.job_status, 1024)  0))
         and bitand(c.flags, 4096) = 0     and (c.class_oid is null       or (c.class_oid is not null       and c.class_oid in (s
    elect d.obj# from sys.scheduler$_class d                           where d.affinity is null))))
    
    

Leave a Reply