4.6. Error Messages and Exit Status

When sed encounters a syntax error, it sends a pretty straightforward error message to standard error; but if it cannot figure out what you did wrong, sed gets "garbled," which you could guess means confused. The exit status that sed returns to the shell, if its syntax is error-free, is a zero for success and a nonzero integer for failure. [2]

[2] For a complete list of diagnostics, see the UNIX man page for sed.

Example 4.4.
1 % sed '1,3v ' file
					sed: -e expression #1, char 4: Unknown command: ''v''

  % echo $status
					(echo $? if using sh or ksh shells)
  2

2 % sed '/John' file
					sed: -e expression #1, char 5: Unterminated address regex

3 % sed 's/1235/g' file
					sed: -e expression #1, char 7: Unterminated 's' command
				

Explanation

  1. The v command is unrecognized by sed. The exit status was 2, indicating that sed exited with a syntax problem.

  2. The pattern /^John is missing the closing forward slash.

  3. The substitution command, s, contains the search string but not the replacement string.

4.6.1. Metacharacters

Like grep, sed supports a number of special metacharacters to control pattern searching. See Table 4.3.

Table 4.3. sed's Regular Expression Metacharacters
MetacharacterFunctionExampleWhat It Matches
^Beginning of line anchor/^love/Matches all lines beginning with love.
$End of line anchor/love$/Matches all lines ending with love.
.Matches one character, but not the newline character/l..e/Matches lines containing an l, followed by two characters, followed by an e.
*Matches zero or more characters/ *love/Matches lines with zero or more spaces, followed by the pattern love.
[ ]Matches one character in the set/[Ll]ove/Matches lines containing love or Love.
[^ ]Matches one character not in the set/[^A–KM–Z]ove/Matches lines not containing Athrough K or M through Z followed by ove.
(..)Saves matched characterss/(love)able/1rs/Tags marked portion and saves it as tag number 1. To reference later, use 1. 1 will be replaced with tagged pattern. May use up to nine tags, starting with the first tag at the leftmost part of the pattern. For example, love is saved in register 1 and remembered in the replacement string. loveable is replaced with lovers.
&Saves search string so it can be remembered in the replacement strings/love/**&**/The ampersand represents the search string. The string love will be replaced with itself surrounded by asterisks; i.e.,love will become **love**.
<Beginning of word anchor/<love/Matches lines containing a word beginning with love.
>End of word anchor/love</Matches lines containing a word that ends with love.
x{m}Repetition of character x, m times/o{5}/Matches if line has 5 o's, at least 5 o's, or between 5 and 10 o's.
x{m,}at least m times
x{m,n}[a]at least m timesat least m and not more than n times

[a] Not dependable on all versions of UNIX or all pattern-matching utilities; usually works with vi and grep.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.152.198