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

This is a discussion on vi - Can vi add Characters in the range of line by command? within the shell forums in Operating Systems category; 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...

Go Back   Database Forum > Operating Systems > shell

Database Forums

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 03-16-2007, 03:29 AM
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
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
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



All times are GMT -4. The time now is 08:44 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Integrated by bbpixel2009 :: jvbPlugin R1013.368.1

Search Engine Friendly URLs by vBSEO 3.1.0
vB Ad Management by =RedTyger=
In an effort to better serve ads to our visitors, cookies are used on Mydatabasesupport.com. For more information, check out our Privacy Policy.