Wednesday 11 December 2013

Ed, the command line editor quick lookup

Ed Command line editor.

Ed is a unix command line editing utility which is interesting as it leads to Grep awk and sed commands to be understood better.
Ed is easier to understand and use.This not a tutorial it for quick lookup for me and those who have not used if from long time and need a little revision. :D enjoy.

consider you are want to edit a file with thousands of words.
 open up the terminal

at prompt type

>ed filename.txt 

nothing will be shown except number of characters in the file.
This command works on just a line at a time.
if you type 'p' and press enter

You will see last line to be printed. Because the ed was pointing at last line. 'p' stands for print.

Entering any number will point to that line number.

 Entering 'd' will delete the line ed is pointing.

rather than moving to a line by line number and then editing .you can specify a regular expression as an address to any particular line containing that regular expression.

suppose u want to delete a line having a word 'regular'  in that line.
then

/regular/d
will delete the line having regular. but this will only delete the line it will encounter the first time containing regular expression

suppose if content of file are,

asdfg
wer
asdfg
rew
asdfg
ewret

and we have processing with ed
the following commands

>3
asdfg
 >/asdfg/d
w
 will save the file as

asdfg
wer
asdfg
rew
ewret

deletion will occur to at next asdfg

g/regular/d
will delete all the lines containing 'regular' word

thats about deletion.

substituting text requires 's'

[address]s/pattern/replacement/flag

where a word pattern will be replaced with replacement. flag can be 'g' for global that is if a g is placed then all the patterns in line will be replaced by replacement.
s/regular/complex/g
above command changes all occurrences on the current line.

address should be specified for this command to act upon more than specific line
/regular/s/regular/complex/g

the above line affects the first line that matches the regular(or address specified).
g/regular/s/regular/complex/g


The family of Unix command Grep is derived from global command Ed as: G/re/p.

 One can write script of edit place them in a separate file and directing them as input to the line editor. For instance, if a series of commands were put in a file named ed-script, following command executes the script:
ed test< ed-script

further you can look at:http://unixhelp.ed.ac.uk/CGI/man-cgi?ed

No comments:

Post a Comment