sed


Metacharacters: (for the first argument of sed)

^   beginning of line
$   end of line
.   one character (in contrast with a period = \.)
*   zero or more characters of the character or expression to the left
[]  any one in the set
[^] not in the set
\<  beginning of word
\>  end of word
(pattern) remember the matched pattern ... pattern = \1 in the 2nd arg of sed
x\{m\} repetition of x exactly m times
x\{m,\} repetition of x for at least m times
x\{m,n\} repitition of x between m and n times
[0-9] one character that is a number
[a-z] one lowercase character that is a letter
[A-Z] one uppercase character that is a letter
[aA-zZ] one character of either case that is a letter
[0-9a-zA-Z] one character that is a number or a letter

Examples

to delete lines from a file: sed '1,3000d' red2wrA.mct > red2wrB.mct
to print certain lines from a file: sed -n '45,50p' filename
to print the nth line from a file: sed -n 3p filename (e.g., to print the third line.)
To delete blank lines: sed '/^$/d' filename
to make a path (or any other string with unusual characters in it) safe for sed: sed 's:[]\[\^\$\.\*\/]:\\\\&:g'
An explanation of how this line works is needed.
to reference a variable from within sed:
  set arg = $n,$n\p
  sed -n "${arg}" $file
If the "$" is meant to be literal, use single quotes instead of double quotes.
To replace every "double" with "float": sed -e 's/double/float/g' doubles.h > floats.h
to replace the first comma in a line with a period: echo $sentence | sed -e 's/,/./'
The "s" means "substitute".   The "-e" means there is a sed script to exectute.
to replace the last comma in a line with a period: echo $sentence | sed 's/\(.*\),/\1./'

The parentheses in the first argument store the enclosed expression.   The "\" tell sed that the parentheses are there to enclose something; they are not characters in the string you want to replace.   The "." matches any character.   The "*" is an example of a sed metacharacter: it means "zero or more of the character or expression to the left".   The "\1" refers back to the expression stored by the parentheses.   So if $sentence == "1,2,3," the string to find and replace would be zero or more arbitrary characters \(.*\) followed by a comma: 1,2,3,   Moving left to right, the matching is said to be "greedy" because at any point it will always match the longest possible string.   Thus, the string chosen for replacement is 1,2,3, and not 1,2, or 1,   The replacement string is all the characters to the left of the comma \1 followed by a period: 1,2,3.


Scripts

To print only the string "124490.sukap004" from the output of qstat: qstat @sukap010 | qlist.sed
#! /bin/sed -f
#  qlist.sed
/sukap/!d
/@/d
s/.*\(......\.sukap...\).*/\1/g
The first command removes every line except ones with the string "sukap".   There are still lines with strings like this left over: "atmpd@sukap010" and "calib@sukap010".   The second command removes these lines by removing any line with an "@" in it.   Now we want to isolate "124490.sukap004" from the other stuff in the same line.   The third line looks for a string consisting of six characters (of any type) followed by ".sukap" followed by three more characters of any type.   Since this expression is enclosed \( expression \) it is stored in \1.   The .* on either side expand the string to include the entire rest of the line.   But when we replace the string, we replace it with only the part included in \( \).

Consecutive commands in sed scripts are applied to the entire input stream before the next command is called.   So echo "222" | example.sed will yield "444" (and not "333".)
#! /bin/sed -f
!  example.sed
s/2/3/g
s/3/4/g

Delete m lines before and n lines after every instance of "example" using cleanup.sed.
This example also illustrates if statements and manipulating the next/previous lines around a string.

Embedding a sed script in a shell script:
        echo $y | sed '{ \
            s/^/     /g \
	    s/\> \<    1.000E-40   1.000E-40   / \
            s/\(\<[^ ]*\>$\)/\1   \1/ \
        }'

Back to Resources