dbaspot
Tags Register FAQ Calendar Search Today's Posts Mark Forums Read

vi - Can vi add Characters in the range of line by command? - shell

This is a discussion on vi - Can vi add Characters in the range of line by command? - shell ; Hi Reader Can vi add Characters in the range of line by command ? e.g. original file Line 1 Line 2 Line 3 Line 4 I want add "!" at the begining of line between line 2 and line 3. ...


Home > Database Forum > Operating Systems > shell > vi - Can vi add Characters in the range of line by command?

Reply

 

LinkBack Thread Tools Display Modes
  #1  
Old 03-16-2007, 03:29 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default vi - Can vi add Characters in the range of line by command?

Hi Reader

Can vi add Characters in the range of line by command ?

e.g.
original file

Line 1
Line 2
Line 3
Line 4
I want add "!" at the begining of line between line 2 and line 3.
to be
Line 1
! Line 2
! Line 3
Line 4

Reply With Quote
  #2  
Old 03-16-2007, 06:37 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: vi - Can vi add Characters in the range of line by command?

moonhk wrote:
> Hi Reader
>
> Can vi add Characters in the range of line by command ?
>
> e.g.
> original file
>
> Line 1
> Line 2
> Line 3
> Line 4
> I want add "!" at the begining of line between line 2 and line 3.
> to be
> Line 1
> ! Line 2
> ! Line 3
> Line 4
>


:2,3 s/^/!/

but vi may not be the best tool for the job. Normally sed or awk would
be the tool of choice for making bulk changes to text files:

sed '2,3 s/^/!/' file
awk 'NR>=2 && NR<=3{printf "!"}1' file

Ed.
Reply With Quote
  #3  
Old 03-16-2007, 09:17 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: vi - Can vi add Characters in the range of line by command?

On 16 Mrz., 08:28, "moonhk" wrote:
> Hi Reader
>
> Can vi add Characters in the range of line by command ?


How is that range defined (by pattern, by line number, by marks)?

>
> e.g.
> original file
>
> Line 1
> Line 2
> Line 3
> Line 4
> I want add "!" at the begining of line between line 2 and line 3.
> to be
> Line 1
> ! Line 2
> ! Line 3
> Line 4


Generally use the substitute command (:s) with an appropriate range
(line numbers, patterns, marks, ...).

:2,3s/^/!/
:/Line 2/,/Line 3/s/^/!/
:'a,'b s/^/!/

(The latter if you have set marks (ma, mb) at the respective lines.)
Or any combination of line numbers, patterns, or marks.
You can also specify the current line by using a dot in one of the
range bounds.

If you happen to use Vim that allows visual marks (use command V to
mark, then type :s/^/!/) which will be displayed as

:'<,'>s/^/!/


Janis

Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
comp.sys.hp.hpux FAQ Database Administrator hp-hpux 0 05-26-2006 12:24 AM
comp.sys.hp.hpux FAQ Database Administrator hp-hpux 0 05-16-2006 12:22 AM
comp.sys.hp.hpux FAQ Database Administrator hp-hpux 0 05-25-2005 12:31 AM
comp.sys.hp.hpux FAQ Database Administrator hp-hpux 0 05-04-2005 12:28 AM
comp.sys.hp.hpux FAQ Database Administrator hp-hpux 0 04-23-2005 12:33 AM


All times are GMT -4. The time now is 03:29 AM.