Writing About Our Experiences With Oracle Databases
Thursday February 9th 2012

Latest Topics

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]

EM12c:Automated discovery of Targets

EM12c:Automated discovery of Targets

In this post we will discuss the Automated discovery of Targets in Enterprise Manager Cloud Control(EM 12c). Once you [Read More]

Em12c:Silent Oracle Management agent Installation

This Post would describe how to install Oracle Management Agent 12c in silent mode. We need to download the agent [Read More]

11gR2:Listener Startup Issues

In this blog post I will be discussing listener startup issues faced in 11gR2 RAC. I will be constantly updating this [Read More]

Get Upgrading: Upgrade to Enterprise Manager Cloud Control 12c

Get Upgrading: Upgrade to Enterprise Manager Cloud Control 12c

This post describes the steps to upgrade an existing 11g Enterprise Manager Grid Control (11.1.0.1.0) to 12c [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

Amit had this to say

Thanks mindinpanic. I have incorporated the comment in article. Appreciate your input. Read the post

mindinpanic had this to say

Sorry for my bad english((( And 6 step is that you must run sqlpus from bin directory of your oracle server Read the post

Claudiomiro Caetano had this to say

Simple and effective. Thanks a lot, it solved my issue. 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 4 other subscribers