gtpo1m68Operations

ZFILE sed-Noninteractive Stream Editor

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




Note:
Throughout this section, the term script refers to a combination of address, subcmd and, where necessary, argument.

-n
displays only the lines that have been coded to print through the p or P editing subcommands. If you do not specify this parameter, all lines of the file you are editing will be written to the standard output (stdout) stream.

-e
allows you to specify more than one piece of input on the command line. You must use the -e parameter when using more than one editing subcommand, an editing subcommand with a scriptfile, and when the specified editing subcommand requires additional input that must be placed on a new line. When entering the editing script in scriptfile rather than on the command line, replace -e with a new line.

-f scriptfile
runs a set of editing subcommands that are stored in a separate file, where scriptfile is the path to the file that contains the editing script you want to run. The script in scriptfile contains a combination of subcommands and the appropriate addresses and arguments. See the subcmd parameter description for more information about the subcommands you can use in scriptfile.

path
is the path to the file you want to read and edit. The results of the editing script are written to stdout, but the original file remains unchanged. If multiple files are specified, the files will be read in the order specified and the result will be written as one concatenated file to stdout.

address
is the address of the line in the specified file the editing subcommand applies to. If an editing subcommand allows up to two addresses, you can specify no address, one address, or two addresses. If no address is specified, the editing subcommand is applied to each line of the specified file. If one address is specified, the editing subcommand applies only to the line matching that address. If two addresses are specified, the addresses are separated by a comma and are interpreted as a range. Each address can be constructed using the following:

n
specifies the line number to which the editing subcommand applies. If the editing subcommand accepts more than one address, you can supply two line numbers to indicate a range. For example 4,6 indicates that you want to edit lines 4 to 6.

$
specifies the last line of the specified file.

/regexp/
specifies an address based on the specified regular expression. All regular expressions must be delimited, which is usually done with a slash (/).

argument
is additional information required by the editing subcommand. See the subcmd parameter description for more information about necessary arguments.

subcmd
is the editing subcommand to apply to the addressed line in the specified file. The ZFILE sed command reads the specified file line by line into a special area known as the pattern buffer. Once a line is in the pattern buffer, its address is compared to the specified address and, if there is a match, the editing subcommand is performed. By default, each line is then written to stdout as the next line of the specified file is read into the pattern buffer, but the file specified in path is not changed. Some editing subcommands use a second area known as the hold space, which serves as temporary storage.

The following table summarizes all the allowable editing subcommands. The columns are labeled as follows:

subcmd
is the subcommand.

Description
describes what the subcommand does.

Argument Information
provides information about arguments that need to be supplied.

Number of Allowable Addresses
indicates the number of addresses that are allowed for that particular subcommand.

Script Syntax
shows the syntax of the address, subcmd and, optionally, argument (collectively known as the script), where [0addr], [1addr], or [2addr] represent the maximum number of addresses allowed and text or label represents the required argument information. (The square brackets ([]) shown in the Script Syntax column are not coded with the addresses.)

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:

n
substitutes only the nth occurrence of old per line. This option is not compatible with the g option.

g
replaces all instances of old throughout the file. This option is not compatible with the n option.

p
writes the changed file to stdout only if a successful substitution occurs.

w file
writes the results of the script to the end of file if a substitution occurs, where file is the path of the file in which to save the results of the script.
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

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.