Sunday, 23 September 2012

ED line editor - tutorial


Ed is a line-oriented text editor.

It can be used interactively or in shell scripts.

Ed is the original text editor for Unix. It was written by Ken Thompson in 1971.

Ed is a small editor with few dependencies that can help you edit some files in situations where no other editor is usable.


INSTALLATION (DEBIAN)


$ sudo aptitude install ed
$ which ed
/bin/ed
$ ed # run ed program.


RUNNING ED EDITOR


$ ed file_name # opens file_name for editing. Changes are not stored until w command is introduced.


COMMAND STRUCTURE


Ed works by accepting commands to edit parts of the file.


Commands usually have this structure:

[ADDRESS [,ADDRESS]]COMMAND[PARAMETERS]

Commands are one letter long.

Address means to which lines command is applied to.

Parameters are passed to command.


TOOGLE THE PROMPT


Our first command: press capital P and press enter

P : toggles showing the prompt (an asterisk appears)

Prompt indicates we are in command mode (vs input mode), we can introduce commands.


QUIT ED EDITOR


When we need to finish running the editor we introduce q command:

q : quit ed editor.


SHELL COMMANDS


! : execute a shell command

E.g:

!ls : executes ls shell command, listing current directory contents.

*!date : executes date command
Sat Sep 22 01:32:33 CEST 2012
!


UNDO COMMAND


If you perform a mistake, you can undo the command:

u : undoes last command


MANIPULATING FILES


Edit a new or another file:
e foo.txt : edit foo.txt file

Save current changes into a file:
w : Writes back changes into current file.

w foo.txt : Writes foo.txt file

Edits output from a shell command:
e !date : edits answer from date shell command.


MOVING THROUGH THE FILE


1 : go to first line (first line becomes current editing line)
$ : go to last line
. : current line
3 : move to line number three.
-2 : moves two lines backwards.
+4 : moves four lines forward.

/regex/ : move to next line where that regex matches

E.g:
/^foo/ : moves to next line which starts with "foo".


?regex? : move to previous line that matches that regex

E.g:
?foo$? : moves to previous line which finishes with "foo".


PRINT LINES


p : prints current line
.p : prints current line

1p : prints first line

1,$p : prints every line in the file
,p : prints every line in the file

-1p : prints line before current one.

.,$ : prints line from current one until last one.


If we want to show numbers of lines:

1,$n : print every line showing its line number.
,n : idem

n : print current line showing its line number.



SUBSTITUTE A REGULAR EXPRESSION


(From info page)
`(.,.)s /RE/REPLACEMENT/'
`(.,.)s /RE/REPLACEMENT/g'
`(.,.)s /RE/REPLACEMENT/N'

Replaces text in the addressed lines matching a regular expression RE with REPLACEMENT.

By default, only the first match in each line is replaced.

If the `g' (global) suffix is given, then every match is replaced.

The N suffix, where N is a postive number, causes only the Nth match to be replaced.


E.g:

2s/foo/bar/ : Change in line 2 "foo" into "bar"

$-1/foo/bar/ : Change "foo" into "bar" in line before last line.

1,$s/foo/bar : Change first "foo" into "bar" in every line

1,$s/\//slash : Change "/" into "slash" in every line

s/foo/bar/g : Change every "foo" into "bar" in current line.



INPUT MODE VS COMMAND MODE


Some commands enter the editor in input mode (e.g: c, i, a), enabling you to introduce data instead of commands.

. : a single period in a line returns to command mode.


CHANGE COMMAND


*3c : Changes line number three. (Enters in input mode)
This is line number three
.
*

Line number three now contains "This is line number three" text, old content is deleted.


APPEND COMMAND


*2a : appends after second line. (Enters in input mode)
foo bar baz # this will be new third line
. # returns to command mode
*

Former third line now is placed in fourth position, new "foo bar baz" line appears in third place.


INSERT COMMAND


*3i : inserts new line number three, moving everyone afterwards one position.
hello
.
*

Former third line is moved to fourth position. "hello" is new third line.


DELETE A LINE


3d : deletes line number three.


MOVE A LINE


1m$ : move first line after the end of the file.

2m0 : move second line after the beginning of the file.

3m1 : move third line after first line.



EXECUTE A COMMAND LIST


g/Regexp/command_list : Finds lines matching Regexp and executes a command list on them.

E.g:
g/foo/s/oo/bar/p # finds every line containing "foo", changes "oo" into "bar", and prints that line.



REFERENCE


$ info ed

Ed home page

Ed (Wikipedia)


0 comentarios: