Softpanorama

May the source be with you, but remember the KISS principle ;-)
Contents Bulletin Scripting in shell and Perl Network troubleshooting History Humor

ed editor

News Syntax Recommended Links Sorting algorithms Recommended Papers Rcut Reference Pipes
Perl re-implemenations uniq sort tr AWK Tips Humor Etc

The external ed command is a line editor. It is the standard text editor for UNIX. A line editor displays a prompt while waiting for you to enter editing commands. It does not display a screen full of text for you to move the cursor around on and make changes. Instead, you display text with an edit command, then use other edit commands to change existing text or add new text.

If you want to change a line in a file choices among standard Unix utilities are ed, awk,  perl or sed. Often using ed for small one line batch corrections is simpler and more convinient you read and write to the same file (Perl also can do that). It is also fast compared to a full-screen editor when you need to perform simple known changes to text -- especially on large files.

The most common modern use of ed it the use in shell scripts. You may also need to use a line editor when you cannot figure out what the proper terminal type is for a certain terminal you are using. Until you find out the terminal type you cannot use the vi screen editor.

Like vi, ed does not actually edit the specified file. It edits a copy of the file it has read into a temporary work file called a buffer. Thus all the edit commands you perform have no effect on the original file until you write the buffer back to the original file using the w command.

The red editor is a restricted version of ed. It only allows editing of files in the current directory. It does not allow you to escape to the shell from within the editor. If you do try to perform these types of commands, red responds with the message "restricted shell."

The tab formatting capability is supported by both ed and red. You must first perform a stty -tabs or a stty tab3, which sets your terminal mode for tab expansion. Then if the first line of the file being edited is a tab stop definition as defined by the tabs command (see Module 130), ed expands all tabs to the corresponding positions. For example, if the first line of your file is

  :t4,8,12,16,20,24,28,32,36

tab stops are set at columns 4, 8, 12, and so on. Tabs embedded with the input of an a, c, and i command are not expanded according to the definition on the first line but, instead, to the default of every eight spaces.

Syntax
ed  [ -p prompt] [ -s ] [ -x ] [ -C ] file
red [ -p prompt] [ -s ] [ -x ] [ -C ] file

BSD (Berkeley)
  ed [ - ] [ -x ] file

Options

The following list describes the options and their arguments that may be used to control how ed functions.

-C Same as -x below except all text read into the buffer is assumed to be encrypted, unless a null key is specified.
-p prompt The -p specifies that a user-defined prompt follows. This allows you to define your own for an internal edit command prompt. For example,
    $ ed -p "infile: " infile
    infile: _
  causes ed to prompt you with infile: instead of the default : (colon) prompt.
-s Suppresses the printing of information type messages. Character counts from the e, r, and w commands are suppressed. The diagnostics from the e and q commands are suppressed. The ! prompt displayed after a !command is also suppressed. On versions prior to SVR4 this was the - option.
-x An encrypted file is to be edited. The file is encrypted when it is saved to disk. It is decrypted when it is read into the editing buffer. You are required to enter an encryption key each time you start editing the file.

BSD (Berkeley)
- Same as System V -s option.
-x Same as System V -x option.

The ed editor has two modes: the input (insert) and command mode. In input mode the editor reads text (input) from your keyboard or a command file and inserts the text into the work buffer. In command mode the editor reads commanda (input) from your keyboard or a command file and performs the requested editing command. To enter insert mode you use one of the insert commands, a, c, or i. To exit insert mode and return to command mode you enter a . at the beginning of the line followed by a Return. When you enter the editor you are placed in command mode on the last line of the buffer.

The following table contains each regular expression and the task that it performs when used inside a pattern. A regular expression consists of regular alphanumeric characters matching themselves and special characters matching certain patterns of text. Regular Expressions are often referred to as REs in UNIX terminology. Thus we use the RE notation for uniformity and briefness.


Alphanumeric
RE Description

c Matches the character c.
string Matches the set of characters string.

Metacharacters

Metacharacters are the special characters used in regular expression patterns that have special meanings. Metacharacters are often referred to as special or magic characters or wild cards.


Special
RE Description

\ Escapes the meaning of a metacharacter.
^ Matches the beginning of the line.
$ Matches the end of the line.
. Matches any single character.
[class] A character class. Matches any one character in the class.
[c1-c2] Match any one of the ASCII characters in the range defined within the brackets.
[^class] Do NOT match any of the ASCII characters listed within the brackets. Ranges may be specified.
RE* Matches zero or more occurrences of the preceding regular expression.
\(\) Group the regular expression RE for later reference; each parenthetical RE is referenced by a corresponding number (see \n). The first group is referenced by \1, the second group by \2, and so on. You can have a maximum of nine groups in one regular expression pattern.
\n Matches the nth grouped \(RE\) within the same regular expression.
RE\{m\} Matches exactly m occurrences of the preceding one-character RE.
RE\{m,\} Matches m or more occurrences of the preceding one-character RE.
RE\{m,n\} Matches m through occurrences of the preceding one-character RE.
// The last RE is used if it has been defined.

To have a metacharacter interpreted as a normal character precede it with a backslash (\).

The ex editor has many ways of addressing lines of text within the current session buffer. The addresses should always precede the command they are affecting.

Basic Addressing The following table describes the basic addressing syntax available for your use.


Address
Symbol Description

  If no address is specified, the current line is affected or used for the address.
. Refers to the current line of the buffer. When you enter ed, the last line is the current line.
$ The last line of the buffer.
; Refers to the current line to the end of the buffer (.,$).
>  
, Refers to line one to the current line (1,.).
<  
n Refers to line number n.
+ Refers to the next line.
+n Refers to n lines forward.
^ Refers to the previous line.
-  
-n Refers to n lines backward.
/pat/ Searches forward for the next line containing the string pat.
?pat? Searches backward for the first previous line containing the string pat.
// Repeats the last forward search.
?? Repeats the last backward search.
'x Refers to line with mark x. The mark must be lowercase.
line Any of the above basic addresses.
 

Some ex commands accept ranges of lines to work with. The following table describes the available ranges.


Address
Symbol Description

a1,a2 Specifies a range of lines beginning at line address a1and continuing through line address a2. Any of the basic addresses are valid. But a1must address a line that precedes address a2. For example,
    /John/,/Mary/ d
  deletes all lines from the line where the string John was found to the line where the string Mary was found. This assumes John resides on a line previous to the line where Mary resides.
a1;a2 Specifies a new current line and the number of lines to offset from the new current line. The address a1 becomes your new current line and a2 becomes the number of lines past the current line. For example,
    10;5 co 50
  copies line 10 through 15 to line 50. The 10 sets your current line to line 10 and the 5 includes the next 5 lines.
range Any of the above addresses.


If NO address is specified in an ed edit command, the current line is usually the default.

 

Command Set

Each edit command you enter in ed must be followed by pressing Return. Some of the edit commands assume default addresses if you do not specify an address on the command line. These default addresses are shown in [ ] (brackets). The following table describes each of the edit commands


Command Description

[.]a
text
.
The append command places you in insert mode. The text you type is entered into the buffer after the address you provide. If you do not provide an address, the current line is assumed. If you specify address 0, the text is inserted before the first line of the buffer. To exit insert mode and return to the command mode you must type a . (period) at the beginning of a line followed by a Return.
C An encryption key is prompted for and is used to decrypt text read and written by all subsequent e, r, and w commands. An empty (null) key turns encryption off. See the -C option.
[.]c The change command allows you to change existing text with new text. The text you enter replaces the text specified by the address. The new text you enter may be any number of lines. For example, if you specify two lines of text to change, you can immediately enter a . (period) and press Return. This would replace the two lines of text with no lines, the same as deleting them. Or you could enter 20 lines of text. It doesn't matter how many lines you specify in the address and how many new lines you enter. The current line becomes the last new line of text. If no new lines were entered, the current line becomes the previous line.
[.,.]d The delete command deletes the specified lines from the buffer. The default address is the current line. The current line becomes the previous line.
e file The edit command causes ed to edit a new file. The current contents of the buffer are deleted. The contents of the specified file is then read into the buffer. If you try using this command and ed responds with a ? then you have made modification to the buffer since the last save to disk. You can override the changes by typing e again. Your changes to the current buffer will be lost and the new file will be edited. You can save the changes by using the w command. Then re-enter the e file command. If no file is specified, the current filename is used. Thus you can re-edit the current file if you really foul things up in the buffer. Just type e Return and e Return again or use the E command.
E file Same as the e command except ed does not check to see if you have outstanding modifications that need to be saved to disk. This command is useful if you decide to start over on the current editing session.
f file The file command changes the current filename to file. If you do not specify a file then the current filename is displayed.
[1,$]g/RE/ed-cmds
  The global command locates all of the lines that contain a string matching the specified regular expression RE. Each of the lines matched are set to the current line and the specified edit command ed-cmd is performed. For example,
    5,25g/SVR4.0/d
  searches from line 5 to line 25 for the string "SVR4.0" and deletes any lines containing a match. If you do not specify an address, the entire buffer is searched. If you do not specify an ed-cmd, the p command is assumed. The g, G, v, and V, commands cannot be used as ed-cmds. Multiple ed-cmds may be specified by placing a \ (backslash) at the end of each line except for the last one. For instance,
    g/UNIX/s//UNIX SVR3.2/\
    i\
    NOTE: A New Release of UNIX\
    .
  locates each line containing "UNIX." A substitution replaces "UNIX" with "UNIX SVR3.2" and a note is inserted before each of these lines.
[1,$]G/RE/ This is the global interactive command. It locates all lines that match the given regular expression RE. Each of the lines matched is set to the current line. The editor displays each line and you are required to enter one and only one ed command. You cannot use the a, c, i, g, G, v, or V command. The command you specify is performed on the current line. After the command is completed the next line containing a match is displayed. To have no command executed on the current line press Return. You can repeat the last command by typing the & (ampersand) key and pressing Return. To abort the interactive global command press the Delete or Break key.
h Displays a brief error message explaining the cause of the last ? diagnostic message. If something is wrong with a command, ed displays a ? (question mark). You must type h and press Return to get an explanation.
H Turns the help feature on and off. If the help feature is on, then a brief message is displayed instead of the ?. If the help feature is off, the ? is displayed and you must use the h command to display the brief explanation.
[.]i The insert command is like the a (append) command, except test the text is inserted before the addressed line instead of after it. You cannot use the i command in an empty file. Nor can you specify address 0. The last inserted line becomes the new current line when you return to the command mode. If no new text is inserted, then the address specified becomes the new current line.
[.,+1]j The join command removes the new-line character at the end of each line. Thus the addressed lines become one long line.
[.]kc The mark command marks the addressed line with letter c. The letter c must be lowercase. You use the k command to set new addresses in the buffer. Then you can reference the address with the 'c address convention. The c may be any character from a to z.
[.,.]l The list command displays the nonprintable characters in a visual representation. Control characters are displayed as ^c or as octal numbers, depending on the function they perform. You can use the l command with any other edit command except the e, f, r, or w command.
[.,.]madr The move command moves the addressed lines to reside after the line specified by adr. You can specify an address of 0 to move the lines to the beginning of the buffer. The address adr cannot be within the specified range of lines being moved. The current line becomes the last line moved.
[.,.]n The number command displays each addressed line preceded by a number and a tab. The numbers are not part of the buffer contents; they are for your information only. The current line becomes the last line displayed. You can use the n command in conjunction with any command except the e, f, r, or w command. If no address is given, the current line is displayed.
[.,.]p The print command displays each addressed line. The current line becomes the last line displayed. You can use the p command in conjunction with any command except the e, f, r, or w command. If no address is given the current line is displayed.
P The prompt command turns the default prompt on or off. The default prompt is an * (asterisk). The prompt is initially turned off.
q The quit command causes ed to exit. If you have saved the contents of the buffer to disk since the last modification, ed exits. If you have outstanding modifications, ed returns a ? or "file not saved" message. You can use the w command to save the buffer contents to disk, then retype q. If you do not want to save the changes to disk, you can repeat the q command again and ed will exit.
Q The nonchecking quit command. This command is equivalent to typing two q commands. The editor does not check for unsaved changes. It immediately exits the buffer whether you have saved modifications to disk or not.
[$]r file The read command reads in the specified file and places the contents after the addressed line. By default, the file is appended to the end of the buffer. If you specify address 0, the file is inserted at the beginning of the buffer. If the file is read in without error, the number of bytes read in are displayed. If you do not specify a file, the currently remembered filename is used. The r command does not reset the remembered filename unless it is the first filename used in the current editing session.
[$]r !command The read command can also read the standard output of any UNIX command. This is like the r file command except the input is read from a command instead of a file. For example,
    r!ls
  reads the output of the ls command executed in the current directory.
[.,.]s/RE/rstr/
[.,.]s/RE/rstr/g
[.,.]s/RE/rstr/n
The substitute command searches each specified line for a matching regular expression RE. If a match occurs, the first RE on the line is replaced with the specified rstr string. If you use the g suffix, all occurrences of RE are replaced on each line (global line substitute). The n suffix causes only the nth occurrence of RE to be replaced on each line. The last line a substitution occurs on becomes the new current line.
  The delimiter character may be any character except a space or new-line. For example, instead of s/old/new/ you can use s#old#new#.
  The & character may be used in the RE or rstr (replacement string) to represent the last RE. For example,
    s/UNIX/&-TM/
  substitutes UNIX to be UNIX-TM.
  The % character may be used in the RE or rstr to represent the last rstr. For example,
    s/OS/%/
  substitutes OS to be UNIX-TM, assuming you just performed the previous example s/UNIX/&-TM/.
[.,.]tadr The copy command works like the move command (m) except a copy of the addressed text is placed after the adr address. The current line becomes the last line of the copied text.
u The undo command reverses the effects of the last text manipulation command.
[1,$]v/RE/ed-cmds
  The global exception command functions like the g command except it selects lines that do not match the specified RE. For example,
    v/^$/p
  displays all lines in the buffer that are not empty lines. The ^$ regular expression means beginning of line followed by end of line, which is equal to an empty line. Refer to the g command for further information.
[1,$]V/RE/ Like the interactive G command, except it selects lines that do not match the specified RE. Refer to the G command for further details.
[1,$]w file The write command writes the specified lines from the buffer to the file. If the write is successful, the editor returns the number of bytes (characters) written to the file. If you do not specify an address range, the entire buffer is saved to disk. If you do not specify a file, the current remembered filename is used. This command does not change the remembered filename unless the specified filename happens to be the first filename used in the editor.
[1,$]W file The same as the w command except the lines are appended to the specified file.
[1,$]w!command
  The same as the w command except the lines are written to the standard input of the specified UNIX command. For example,
    10,50w!pr -t2
  writes lines 10 through 50 to the UNIX pr command. The pr command formats the input into two-column output.
X Same as -C but an educated guess is made as to whether or not the text read (in by e and r commands) is encrypted.
[$]= The line number command displays the line number of the specified address. If no address is given, the last line is used, thereby giving you the total number of lines in the file. Another common use is the .= format which returns the current line number.
!command The escape command escapes from the editor and invokes the shell to execute the specified UNIX command. For example,
    !grep mylogin /etc/passwd
  greps for the string mylogin from the passwd file. This command does not read the output of the command into the buffer. To do that you must use the r! command.
  The % character may be used in the command to represent the currently remembered filename. For example,
    !%
  executes the current file you are editing. This is useful if you are editing a shell script and need to check it before exiting the editor.
Address Commands
<CR> Pressing Return on a line by itself displays the next line and increments the current line number.
n A number command may be used to change the current line position to the given line number.
. The dot represents the current line and thus displays the current line when used by itself.
;
>
The ; and > characters represent addresses .,$, which is from the current line to the end of the buffer.
,
<
The , and < characters represent addresses 1,. which is from the first line to the current line.
& The repeat command repeats the last command you entered.

In most cases you can only specify one command per line. The l, n, and p commands are exceptions to this rule. You can specify these three commands with other commands. This is because they are all display oriented commands. But you cannot use them with the e, E, f, or w commands.

ed has very brief and usually useless error messages. The editor uses several files and directories while you are editing.

/var/tmp The directory where the temporary file buffer is stored.
TMPDIR The shell variable containing the name of the directory to use instead of the default /var/tmp directory. If the variable is set, the specified path is used; otherwise, /var/tmp is used.
ed.hup The contents of the buffer are saved to this file if the line hangs up (you get disconnected from the system). This is usually caused by a phone line or modem problem, although even direct cabling has been know to be pulled loose from the computer or terminal by clumsy people.



Etc

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available in our efforts to advance understanding of environmental, political, human rights, economic, democracy, scientific, and social justice issues, etc. We believe this constitutes a 'fair use' of any such copyrighted material as provided for in section 107 of the US Copyright Law. In accordance with Title 17 U.S.C. Section 107, the material on this site is distributed without profit exclusivly for research and educational purposes.   If you wish to use copyrighted material from this site for purposes of your own that go beyond 'fair use', you must obtain permission from the copyright owner. 

ABUSE: IPs or network segments from which we detect a stream of probes might be blocked for no less then 90 days. Multiple types of probes increase this period.  

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Haterís Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least


Copyright © 1996-2016 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License.

The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to make a contribution, supporting development of this site and speed up access. In case softpanorama.org is down you can use the at softpanorama.info

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

Last modified: June 04, 2016