Writing About Our Experiences With Oracle Databases
Friday September 3rd 2010

Latest Topics

Effect Of Multiple SHMMAX Settings

Last week I saw a warning message at database startup time saying: WARNING: EINVAL creating segment of size [Read More]

Finding blogging tough? Just copy and do find and replace

Finding blogging tough? Just copy and do find and replace

Well this is motto followed by Guenadi Jilevski for his blog http://gjilevski.wordpress.com and he seems to get result [Read More]

Physical Corruption: ORA-1578 part 3

Recently we encountered a block corruption issue in 10g database on Linux x86 64 bit and using ASM for storing database [Read More]

Cloning RAC Database with RMAN fails with error

Steps for cloning a RAC database  with RMAN is similar to cloning a single instance database. But while using rman [Read More]

DBMS_SCHEDULER jobs not running?

I came across this posting on OTN which lists down things to check in case your scheduler job is not . This is quite [Read More]

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.. :)
  • Share/Bookmark
Related Tags: , ,

3 Comments for “Use awk/sed in vi”

  • Chris Neumueller says:

    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.

  • Amit says:

    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

  • Amit says:

    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 Comment

Recent Comments

Ron Gordon had this to say

I had a simillar ORA-15020: discovered duplicate ASM disk issue, our sysadmin determined that there was a BAD Read the post

Can a security group be a member of another group? had this to say

install Oracle and have so many problems with it. My goal is to install it according to this link Read the post

M Tanvir had this to say

Hi Amit, really its great response, bcoz i just new with oracle. Thanks... Read the post

Aman.... had this to say

Hahaha aur sab se badi baat, he is not even ready to accept! jigar chahiye bhai yeh karne aur fir na maanne ke liye :D . Read the post

Dev had this to say

Hi Amit, Thanks for quick reply. It looks fine on my HTC Desire ( Android ) ,so no need to worry about Read the post