Difference between revisions of "sed sed"
From Noah.org
Jump to navigationJump to searchm |
|||
Line 3: | Line 3: | ||
== POSIX regular expression character classes == | == POSIX regular expression character classes == | ||
− | POSIX regex character classes are used inside regex chracter classes, so you often see them with double square brackets. For example, [[:digit:]]. | + | POSIX regex character classes are used inside regex chracter classes, so you often see them with double square brackets. For example, <nowiki>[[:digit:]]</nowiki>. |
<pre> | <pre> | ||
[:digit:] digits 0 through 9 | [:digit:] digits 0 through 9 |
Revision as of 11:34, 12 July 2012
Contents
POSIX regular expression character classes
POSIX regex character classes are used inside regex chracter classes, so you often see them with double square brackets. For example, [[:digit:]].
[:digit:] digits 0 through 9 [:xdigit:] hex digits 0 through f, equivalent to [0-9a-fA-F] [:alpha:] alpha characters [a-zA-Z] (may include other letters depending on locale language) [:alnum:] alphanumeric charactera [0-9a-zA-Z] (may include other letters depending on locale language) [:space:] all whitespace characters (space, tab, NL, FF, VT, CR), equivalent to [\s] [:blank:] space and TAB characters (compare to [:space:] above) [:punct:] punctuation . , " ' ? ! ; : # $ % & ( ) * + - / < > = @ [ ] \ ^ _ { } | ~ [:print:] printable characters, including whitespace. [:graph:] printable character, excluding whitespace (SPACE, TAB), equivalent to [\W\ [:upper:] alpha characters [A-Z] [:lower:] alpha characters [a-z] [:cntrl:] control characters (NL CR LF TAB VT FF NUL SOH STX EXT EOT ENQ ACK SO SI DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC IS1 IS2 IS3 IS4 DEL)
This finds a line in a config file and sets a value to 1 even if that line is commented out.
sed -e 's/^[#]\?[[:blank:]]*noswap[[:blank:]]*=.*/noswap = 1/' /etc/xen-tools/xen-tools.conf
find part of a line, substitute, keep rest of line intact
sed -i -e "s/^#DatabaseDirectory \(.*\)/DatabaseDirectory \\1/" myfile.txt
find pattern in a line, insert new line before it
This finds line that begins with exit(ignore leading spaces), then insert 'authdarmond start' before it.
sed -i -e "/^\\s*exit/i authdaemond start" /etc/rc.local
find pattern and append line
This will add QMAILQUEUE environment variable to qmail-smtpd/run. Notice how the replace pattern spans multiple lines using backslash to escape.
sed -i -e "/#\!\/bin\/sh/a\ QMAILQUEUE="/var/qmail/bin/qmail-scanner-queue.pl\" ; export QMAILQUEUE" /var/qmail/supervise/qmail-smtpd/run
print everything between two patterns
Use ranges for this. A range can take a line number or a pattern.
sed -n "/begin_pattern/,/end_pattern/p" myfile.txt
search and replace in multiple files
This will perform a search and replace on all files in a directory tree:
find ./ -type f -exec sed -i -e "s/find_this_text/replace_with_this_text/g" '{}' \;
sed documentation
This is one of the best sed documents I found: Grymoire Sed