gtpo1m68 | Operations |
Use this command to apply editing subcommands to a file and to display the results on the screen. Once the editing script is completed, the results are written to the standard output (stdout) stream, but the original file remains unchanged.
Requirements and Restrictions
Format
|
The following table summarizes all the allowable editing subcommands. The columns are labeled as follows:
In this table, the syntax of each subcommand is shown as it would appear in scriptfile. When using these subcommands on the command line, refer to the syntax diagram for the location of the necessary -e, which is replaced by a new line when using these subcommands in scriptfile.
Table 11. ZFILE sed Command Editing Subcommand
subcmd | Description | Argument Information | Number of Allowable Addresses | Script Syntax |
---|---|---|---|---|
a\ | Appends lines of text to the specified file. The ZFILE sed command writes the text after completing all other specified editing subcommands for that line and before reading the next line. | text is the text you want to add. The first line that does not end with a backslash (\) character is the end of the text you are appending, but the \ is not handled as part of the text. | 0 or 1 |
[1addr]a\ text |
b | Branches to the specified label. If no label is specified, the branch goes to the end of the script. When specifying a label, do not put any additional white space after the label. | label is the location in which to branch. See the label subcommand for more information. | 0, 1, or 2 |
[2addr]b label |
c\ | Replaces the pattern buffer with the supplied text. | text is the text you want to use in the replacement. | 0, 1, or 2 |
[2addr]c\ text |
d | Deletes the contents of the pattern buffer. The script is restarted on the next line of the specified file. | No arguments allowed. | 0, 1, or 2 |
[2addr]d |
D | Deletes the information held in the pattern buffer up to and including the first new-line character (\n). The script is then restarted from the beginning and is applied to the text in the pattern buffer. | No arguments allowed. | 0, 1, or 2 |
[2addr]D |
g | Replaces the pattern buffer with the contents of the hold space. | No arguments allowed. | 0, 1, or 2 |
[2addr]g |
G | Appends the pattern buffer with a new-line character (\n) and then the contents of the hold space without replacing the entire line. | No arguments allowed. | 0, 1, or 2 |
[2addr]G |
h | Copies the pattern buffer into the hold space. Any text previously held is replaced. | No arguments allowed. | 0, 1, or 2 |
[2addr]h |
H | Appends the hold space with a new-line character (\n) and then the text of the pattern buffer. No text is replaced. | No arguments allowed. | 0, 1, or 2 |
[2addr]H |
i\ | Inserts the specified text. | text is the text to insert. | 0 or 1 |
[1addr]i\ text |
l | Writes the pattern buffer to stdout so nonprintable characters are visible. The end of a line is represented by the dollar sign character ($), and the characters \\, \a, \b, \f, \r, \t, and \v are printed as escape sequences. Other nonprintable characters will be written as a 2-digit hexadecimal number for each byte. | No arguments allowed. | 0, 1, or 2 |
[2addr]l |
n | Skips the pattern buffer and moves to the next input line. If the -n parameter is not specified, the skipped line will be printed to stdout. For example, this parameter could be used to delete every third line of the file before writing it to stdout by entering ZFILE sed -e 'n' -e 'n' -e 'd' /u/file1 on the command line. | No arguments allowed. | 0, 1, or 2 |
[2addr]n |
N | Appends the next line of input to the current line in the pattern buffer with a new-line character (\n) separating the original material and the appended material. As a result, the current line number changes. | No arguments allowed. | 0, 1, or 2 |
[2addr]N |
p | Prints the text in the pattern buffer to stdout. When the -n parameter is not specified, this is the default. | No arguments allowed. | 0, 1, or 2 |
[2addr]p |
P | Similar to the p subcommand except that it prints the text in the pattern buffer only up to and including the first new-line character (\n). | No arguments allowed. | 0, 1, or 2 |
[2addr]P |
q | Quits the ZFILE sed command by skipping the remainder of the script and reading no more input lines | No arguments allowed. | 0 or 1 |
[1addr]q |
r | Reads text in the specified argfile and writes that text to stdout before reading the next input line. If argfile does not exist or cannot be read, it is handled as an empty file. | argfile is the path to the file that contains the text you want read at the specified address. | 0 or 1 |
[1addr]r argfile |
s | Substitutes old text with new text. | Indicates which pieces of text you want to remove (old) as well
as what you want put in its place (new). By default, the
substitution will only take place on the first occurrence of old on
each line. You can change this with a combination of following
options:
| 0, 1, or 2 |
[2addr]s/old/new/[gnp][wfile] You can use any single printable character, other than a space or new-line character (\n), instead of a slash (/) to delimit old and new. The delimiter itself may appear as a literal character in old or new if it is preceded with a backslash (\). |
t | Branches to the indicated label only if a successful substitution has occurred since either reading the last input line or running the last t subcommand. If you do not specify a label, the branch goes to the end of the script. | label is the location in which to branch. See the label subcommand for more information. | 0, 1, or 2 |
[2addr]t label |
w | Writes the pattern buffer to the end of file. | file is the path to the file to append. | 0, 1, or 2 |
[2addr]w file |
x | Exchanges the text in the hold space with the contents of the specified line. | No arguments allowed. | 0, 1, or 2 |
[2addr]x |
y | Replaces any character that occurs in string1 with the corresponding character in string2 on the specified line. | Specify string1 and string2, where string1 is the string to search for and string2 is the replacement string. The strings must be the same length. | 0, 1, or 2 |
[2addr]y/string1/string2 You can use any character other than a backslash or new-line character (\n) instead of the slash to delimit the strings. |
= | Includes the decimal value of the current line number when writing to stdout. | Not applicable | 0 or 1 | [1addr]= |
Additional Tools for Creating an Editing Script | ||||
{subcmd} | The braces ({}) allow you to group within your script, where subcmd is the group of editing subcommands to group together. The ZFILE sed command runs the script as a group only on those lines that match the specified address. | Not applicable. | 0, 1, or 2 |
[2addr]{subcmd} |
! | Runs the specified subcommands only on those lines not matching the specified address. | subcmd is the editing subcommand you want to run on the lines not matching the specified address. | Determined by which editing sub- command you are using. |
[2addr]!subcmd |
# | Allows you to code comments into your script. The single exception is when #n is on the first line in scriptfile, which is interpreted to mean the same as the -n parameter on the command line. An empty script line is also handled as a comment. | Not applicable. | Not applicable. |
[0addr]# comment |
label | Designates the name of the location in which to branch and must be preceded by a colon (:). | label is used with either the b subcommand or the t subcommand. See the b and t subcommands for more information. | Not applicable. |
[0addr]: label |
Additional Information
ZFILE HELP sed
ZFILE HELP
ZFILE ?
ZFILE ls -l | grep JanThe result is filtered output from the ZFILE ls command displaying only the lines containing the word Jan in any position.
You can use pipes only with a combination of ZFILE commands where the command on the left-hand side of the pipe provides data through stdout and the right-hand side accepts data through stdin.
You can redirect the standard output (stdout) stream from the display terminal to a file by specifying one of the redirection characters (> or >>) followed by the file name to which you want the output written. The > character writes the output to a file. The >> character appends the output to an existing file.
You can redirect the standard error (stderr) stream from the display terminal to a file by specifying one of the redirection characters (2> or 2>>) followed by the file name to which you want the error output written. The 2> character writes the error output to a file. The 2>> character appends the error output to an existing file.
User: ZFILE echo \$PATH is $PATH System: $PATH is /bin:/usr/bin:.
left angle bracket (<) | right angle bracket (>) | ampersand (&) |
backquote (`) | backslash (\) | dollar sign ($) |
double quotation mark (") | new-line (\n) | left parenthesis ( ( ) |
right parenthesis ( ) ) | semicolon (;) | single quotation mark (') |
blank space | tab | vertical bar (|) |
asterisk (*) | equal sign (=) | left square bracket ( [ ) |
number sign (#) | question mark (?) | tilde (~) |
Examples
The following example appends the text Sandy Davis, 954 East Harvard Drive, Denver, CO after the third line of the file named list, which contains the following:
Emily Adams, 256 Middletown Road, Plymouth, MA John Baker, 333 Union Road, Plymouth, MA Alice and David Smith, 345 East Broadway, Richmond, VA Brian Adams, 2000 Bayshore Court, San Diego, CA Sally Jones, 7333 West 6th Street, Boston, MA
+---------------------------------------------------------------------------------------------+ |User: ZFILE sed -e '3a\' -e 'Sandy Davis, 954 East Harvard Drive, Denver, CO' list | | | |System: FILE0001I 08:14:31 START OF DISPLAY FROM sed -e '3a\' -e 'Sandy Davis, 954 E... | | Emily Adams, 256 Middletown Road, Plymouth, MA | | John Baker, 333 Union Road, Plymouth, MA | | Alice and David Smith, 345 East Broadway, Richmond, VA | | Sandy Davis, 954 East Harvard Drive, Denver, CO | | Brian Adams, 2000 Bayshore Court, San Diego, CA | | Sally Jones, 7333 West 6th Street, Boston, MA | | END OF DISPLAY | +---------------------------------------------------------------------------------------------+
The following example deletes line 2 to line 4 from file list. The contents of list are shown in the previous example.
+--------------------------------------------------------------------------------+ |User: ZFILE sed '2,4d' list | | | |System: FILE0001I 08:14:31 START OF DISPLAY FROM sed '2,4d' list | | Emily Adams, 256 Middletown Road, Plymouth, MA | | Sally Jones, 7333 West 6th Street, Boston, MA | | END OF DISPLAY | +--------------------------------------------------------------------------------+
The following example substitutes the word Massachusetts for the letters MA in list. The contents of file list are shown in the first example.
+---------------------------------------------------------------------------------------------+ |User: ZFILE sed 's/MA/Massachusetts/' list | | | |System: FILE0001I 08:14:31 START OF DISPLAY FROM sed 's/MA/Massachusetts/' list | | Emily Adams, 256 Middletown Road, Plymouth, Massachusetts | | John Baker, 333 Union Road, Plymouth, Massachusetts | | Alice and David Smith, 345 East Broadway, Richmond, VA | | Brian Adams, 2000 Bayshore Court, San Diego, CA | | Sally Jones, 7333 West 6th Street, Boston, Massachusetts | | END OF DISPLAY | +---------------------------------------------------------------------------------------------+
The following example counts the number of lines in file list. The -n parameter suppresses the automatic output of each input line. The contents of file list are shown in the first example.
+--------------------------------------------------------------------------------+ |User: ZFILE sed -n '$=' list | | | |System: FILE0001I 08:14:31 START OF DISPLAY FROM sed -n '$=' list | | 5 | | END OF DISPLAY | +--------------------------------------------------------------------------------+
The following example runs a script located in scriptfile against file list2. The script, which uses the N, b, d, and s subcommands, joins all lines ending with a backslash (\) and deletes comment and blank lines. The contents of list2 are:
# text file to show joining lines # also contains blank lines and comments Emily Adams, 256 Middletown Road, Plymouth, MA John Baker, 333 Union Road, Plymouth, \ MA Alice and David Smith, 345 East Broadway, Richmond VA Brian Adams, 2000 Bayshore Court, San Diego \ CA Sally Jones, 7333 West 6th Street, Boston, MA
The contents of scriptfile are:
# /^#/d delete all lines beginning with '#' (comments?) # /^$/d delete all empty lines (/./!d could be used instead) # :join # /\\$/{ # N # s/\\\n// # bjoin # } # would join all lines ended with '\', after deleting # the '\' itself # /^#/d /^$/d :join /\\$/{ N s/\\\n// bjoin }
+--------------------------------------------------------------------------------+ |User: ZFILE sed -f scriptfile list2 | | | |System: FILE0001I 08:14:31 START OF DISPLAY FROM sed -f scriptfile list2 | | Emily Adams, 256 Middletown Road, Plymouth, MA | | John Baker, 333 Union Road, Plymouth, MA | | Alice and David Smith, 345 East Broadway, Richmond, VA | | Brian Adams, 2000 Bayshore Court, San Diego, CA | | Sally Jones, 7333 West 6th Street, Boston, MA | | END OF DISPLAY | +--------------------------------------------------------------------------------+
The following example runs a script located in scriptfile2 against file index.html. The purpose of the script is to look through a file and display all the URLs referenced in the file. The contents of scriptfile2 are:
# # list URLs and associated comments in HTML # # NOTES # 1. Results are formatted as: URL<tab>comment # 2. ALT values spanning more than one line have leading whitespace reduced # to a single space. # :top # imagemap links... /<[Aa][Rr][Ee][Aa]/ { :imagemap ## read up to end of directive />/ !{ N s/[ ]*\n[ ]*/ / b imagemap } ## move remainder of line into hold space h x s/[^>]*>// x s/>.*/>/ ## reformat directive as: <href=foo alt="blah"> s/ [Aa][Ll][Tt]=/ alt=/ s/ [Hh][Rr][Ee][Ff]=/ href=/ # ensure ALT attribute is present / alt=/ ! s/>/ alt="">/ # ensure both attributes are quoted s/ alt=\([^" >]\{1,\}\(/ alt="\1"/ s/ href=\([^" >]\{1,\}\(/ href="\1"/ # ensure HREF precedes ALT s/\( alt="[^"]*"\(\(.*\(\( href="[^"]*"\(>/\3\2\1>/ s/^[^<]*// # print result as: URL<tab>comment s/.*href="\([^"]*\(" alt="\([^"]*\(">/\1 \2/p # continue processing with remainder of line s/.*// x b top } ## anchor links... /<[Aa][^a-zA-Z0-9]\{0,1\}/ { :anchor # read up to closing tag /<\/[Aa]>/ !{ N s/[ ]*\n[ ]*/ / b anchor } s/"//g G s/<\/[Aa]>\(.*\(\(.\($/\2\1/ # s/^[^<]*\(<[Aa] \(/\1/ # only continue if this anchor contains HREF /<[Aa][^>]* [Hh][Rr][Ee][Ff]=.*\n/ ! { s/.*\n//1 b top } s/.* [Hh][Rr][Ee][Ff]=\([^>]*\(>[ ]*\(.*\n\(/\1 \2/ s/<[^>]*>//g P s/.*\n//1 b top }
+---------------------------------------------------------------------------------------------+ |User: zfile sed -nf url.t /usr/local/apache/htdocs/index.html | | | |System: FILE0001I 08.05.41 START OF DISPLAY FROM sed -nf url.t /usr/local/apache/htd... | | http://www.apache.org/. Apache.. | | manual/index.html. documentation.. | | END OF DISPLAY | +---------------------------------------------------------------------------------------------+
Related Information
See Information Technology--Portable Operating System Interface for Computer Environments (POSIX) for more information about the POSIX standards.