| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
|
| 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
|
| 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. |
|
#3
|
| On 16 Mrz., 08:28, "moonhk" > 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 |
![]() |
| Thread Tools | |
| Display Modes | |