Writing About Our Experiences With Oracle Databases
Tuesday May 22nd 2012

Latest Topics

Using current_scn for RMAN incremental ? Think again..

I am bloging about experience we had using rman incremental backup for syncing standby database. We had a standby [Read More]

Poll on Sql Plan Management

Dominic is conducting poll on SPM and Sql Profiles usage on his website. Link can be found here  I have been using SPM [Read More]

Oracle Direct connector for HDFS

Some time back oracle had announced Oracle Big Data appliance. Along with this Oracle released Big Data Connectors to [Read More]

ORA-7445 core dump [kokscold()+849] from EM 12c

The first problem reported by my EM 12c is an ORA-7445 error. After checking the alert log following is the exact [Read More]

Copying Oracle Scheduler jobs with Arguments

Oracle provides dbms_scheduler.copy_job function to copy oracle scheduler jobs which works great. But problem is that [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
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

terkel had this to say

I still dont get it, so if someone get the ewallet.p12 cwallet.sso files and the connect string (simply by reading/copy Read the post

Amit had this to say

hi Bhushan, Em should be pretty easy. I will try to do it in my test setup and document it. cheers Amit Read the post

bhushan had this to say

Hey Amit, Any Document to Setup the Dataguard using EM12C? Any tips any suggestion? Regards Bhushan Read the post

More from category

Cluster SSH tool Utility
Cluster SSH tool Utility

Many times you come across scenario’s when you wish to open multiple ssh windows and execute same commands.e.g [Read More]

Input is too long (> 2499 characters) – line ignored

There are times when you observe above mentioned error while creating view and mview from mview definition taken from [Read More]

Effect of OS Terminal Setting “STTY” on Oracle Database

Few days back, while trying to create a synonym over dblink I experienced following error: SQL> create synonym [Read More]

Easy and Free X-Windowing Setup with Cygwin

Today I had to carry out Oracle client Installation on one of the server. Normally I use vncviewer to connect to one of [Read More]

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 11 other subscribers

asd123