« ServerTokens ProductOnly 和 ServerSignature Off 微调 Apache 的返回信息 | Main | Rainlendar - 一个免费、开源的桌面日历 »
May 31, 2005
sed 一个小强文本操作工具 替换、插入新语句打开httpd server-status mod_status
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明。 https://windtear.net/archives/2005/05/31/000677.html http://windtear.net/archives/2005/05/31/000677.html sed 主页 http://sed.sourceforge.net/ http://sed.sourceforge.net/grabbag/ 目前最新版本: ftp://ftp.gnu.org/gnu/sed/sed-4.1.4.tar.gz ftp://ftp.gnu.org/gnu/sed/ 原来的gnu的主页只停留在 3.02 止步不前了:( http://www.gnu.org/software/sed/sed.html http://www.gnu.org/software/sed/manual/sed.html 这里给出一个demo 用 -i 文件本身替换 (-i[suffix], --in-place[=suffix]) 替换、插入新语句打开httpd 配置 httpd.conf 里面的 server-status (需要mod_status模块支持) (当然用 perl -pi -e 等也可以方便替换) sed -i -e 's@#ExtendedStatus On@ExtendedStatus On@g' httpd.conf sed -i -e 's@#<Location /server-status>@<Location /server-status>\n\tSetHandler server-status\n\tOrder deny,allow\n\tDeny from all\n\tAllow from 59.66.110.35\n</Location>\n#<Location /server-status>@g' httpd.conf sed 小巧强大(小强) 可以进行很灵活的文本操作 作为一个常用的脚本调用接口 sed 举足轻重 替换脚本部分可以支持很多选项 个人喜好不同 干脆 man 出来 前面提到的主页有很多文档 样例 脚本等可以参考 % man sed | col -b > sed.txt % cat sed.txt SED(1) User Commands SED(1) NAME sed - manual page for sed version 4.0.7 SYNOPSIS sed [OPTION]... {script-only-if-no-other-script} [input-file]... DESCRIPTION Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors. -n, --quiet, --silent suppress automatic printing of pattern space -e script, --expression=script add the script to the commands to be executed -f script-file, --file=script-file add the contents of script-file to the commands to be executed -i[suffix], --in-place[=suffix] edit files in place (makes backup if extension supplied) -l N, --line-length=N specify the desired line-wrap length for the `l' command -r, --regexp-extended use extended regular expressions in the script. -s, --separate consider files as separate rather than as a single continuous long stream. -u, --unbuffered load minimal amounts of data from the input files and flush the output buffers more often --help display this help and exit -V, --version output version information and exit If no -e, --expression, -f, or --file option is given, then the first non-option argument is taken as the sed script to interpret. All remaining arguments are names of input files; if no input files are specified, then the standard input is read. E-mail bug reports to: bonzini@gnu.org . Be sure to include the word ``sed'' somewhere in the ``Subject:'' field. COMMAND SYNOPSIS This is just a brief synopsis of sed commands to serve as a reminder to those who already know sed; other documentation (such as the texinfo document) must be consulted for fuller descriptions. Zero-address ``commands'' : label Label for b and t commands. #comment The comment extends until the next newline (or the end of a -e script fragment). } The closing bracket of a { } block. Zero- or One- address commands = Print the current line number. a \ text Append text, which has each embedded newline preceded by a back- slash. i \ text Insert text, which has each embedded newline preceded by a back- slash. q Immediately quit the sed script without processing any more input, except that if auto-print is not disabled the current pattern space will be printed. Q Immediately quit the sed script without processing any more input. r filename Append text read from filename. R filename Append a line read from filename. Commands which accept address ranges { Begin a block of commands (end with a }). b label Branch to label; if label is omitted, branch to end of script. t label If a s/// has done a successful substitution since the last input line was read and since the last t or T command, then branch to label; if label is omitted, branch to end of script. T label If no s/// has done a successful substitution since the last input line was read and since the last t or T command, then branch to label; if label is omitted, branch to end of script. c \ text Replace the selected lines with text, which has each embedded newline preceded by a backslash. d Delete pattern space. Start next cycle. D Delete up to the first embedded newline in the pattern space. Start next cycle, but skip reading from the input if there is still data in the pattern space. h H Copy/append pattern space to hold space. g G Copy/append hold space to pattern space. x Exchange the contents of the hold and pattern spaces. l List out the current line in a ``visually unambiguous'' form. n N Read/append the next line of input into the pattern space. p Print the current pattern space. P Print up to the first embedded newline of the current pattern space. s/regexp/replacement/ Attempt to match regexp against the pattern space. If success- ful, replace that portion matched with replacement. The replacement may contain the special character & to refer to that portion of the pattern space which matched, and the special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp. w filename Write the current pattern space to filename. W filename Write the first line of the current pattern space to filename. y/source/dest/ Transliterate the characters in the pattern space which appear in source to the corresponding character in dest. Addresses Sed commands can be given with no addresses, in which case the command will be executed for all input lines; with one address, in which case the command will only be executed for input lines which match that address; or with two addresses, in which case the command will be exe- cuted for all input lines which match the inclusive range of lines starting from the first address and continuing to the second address. Three things to note about address ranges: the syntax is addr1,addr2 (i.e., the addresses are separated by a comma); the line which addr1 matched will always be accepted, even if addr2 selects an earlier line; and if addr2 is a regexp, it will not be tested against the line that addr1 matched. After the address (or address-range), and before the command, a ! may be inserted, which specifies that the command shall only be executed if the address (or address-range) does not match. The following address types are supported: number Match only the specified line number. first~step Match every step'th line starting with line first. For example, ``sed -n 1~2p'' will print all the odd-numbered lines in the input stream, and the address 2~5 will match every fifth line, starting with the second. (This is an extension.) $ Match the last line. /regexp/ Match lines matching the regular expression regexp. \cregexpc Match lines matching the regular expression regexp. The c may be any character. GNU sed also supports some special 2-address forms: 0,addr2 Start out in "matched first address" state, until addr2 is found. This is similar to 1,addr2, except that if addr2 matches the very first line of input the 0,addr2 form will be at the end of its range, whereas the 1,addr2 form will still be at the beginning of its range. addr1,+N Will match addr1 and the N lines following addr1. addr1,~N Will match addr1 and the lines following addr1 until the next line whose input line number is a multiple of N. REGULAR EXPRESSIONS POSIX.2 BREs should be supported, but they aren't completely because of performance problems. The \n sequence in a regular expression matches the newline character, and similarly for \a, \t, and other sequences. BUGS E-mail bug reports to bonzini@gnu.org. Be sure to include the word ``sed'' somewhere in the ``Subject:'' field. Also, please include the output of ``sed --version'' in the body of your report if at all possi- ble. COPYRIGHT Copyright ?2003 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, to the extent permitted by law. SEE ALSO awk(1), ed(1), grep(1), tr(1), perlre(1), sed.info, any of various books on sed, the sed FAQ (http://sed.sf.net/grabbag/tutorials/sed- faq.html), http://sed.sf.net/grabbag/. The full documentation for sed is maintained as a Texinfo manual. If the info and sed programs are properly installed at your site, the com- mand info sed should give you access to the complete manual. sed version 4.0.7 April 2003 SED(1) -----> Redhat 7.3 带的到 3.02.11 1/RedHat/RPMS/sed-3.02-11.i386.rpm Name : sed Relocations: /usr Version : 3.02 Vendor: Red Hat, Inc. Release : 11 Build Date: Fri 05 Apr 2002 05:26:22 PM CST Install date: (not installed) Build Host: daffy.perf.redhat.com Group : Applications/Text Source RPM: sed-3.02-11.src.rpm Size : 72963 License: GPL Packager : Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla> Summary : A GNU stream text editor. Description : Sed (Stream EDitor) is a stream or batch (non-interactive) editor. Sed takes text as input, performs an operation or set of operations on the text, and outputs the modified text. The operations that sed performs (substitutions, deletions, insertions, etc.) can be specified in a script file or from the command line. /bin/sed /usr/share/doc/sed-3.02 /usr/share/doc/sed-3.02/ANNOUNCE /usr/share/doc/sed-3.02/BUGS /usr/share/doc/sed-3.02/NEWS /usr/share/doc/sed-3.02/README /usr/share/doc/sed-3.02/TODO /usr/share/info/sed.info.gz /usr/share/man/man1/sed.1.gz 3.02.11 还不支持 -i 参数 也就是文件内部替换 AS3 带的是 4.0.7-3 (Build Date: Fri 27 Jun 2003 03:19:31 AM CST) 目前最新的是 4.1.4 版本 lftp ftp.gnu.org:/gnu/sed> ls -ltr -rw-r--r-- 1 0 0 1389 May 23 1993 sed-1.15-1.16.diff.gz -rw-r--r-- 1 0 0 350 May 31 1993 sed-1.17-1.18.diff.gz -rw-r--r-- 1 0 0 93301 Jun 01 1993 sed-1.18.tar.gz -rw-r--r-- 1 0 0 114600 May 13 1994 sed-2.05.tar.gz -rw-r--r-- 1 0 0 311172 Jul 22 1997 sed-2.05.bin.hpux10 -rw-r--r-- 1 0 0 263949 Jul 23 1998 sed-3.01.tar.gz -rw-r--r-- 1 0 0 264236 Aug 02 1998 sed-3.02.tar.gz -rw-r--r-- 1 0 0 3179 Aug 09 1998 sed-3.01-3.02.diff.gz -rw-r--r-- 1 0 0 477 Aug 10 1999 sed-2.05.bin.README -rw-r--r-- 1 0 0 79542 Nov 05 2002 sed-4.0-4.0.1.diff.gz -rw-r--r-- 1 0 0 10065 Nov 19 2002 sed-4.0.1-4.0.2.diff.gz -rw-r--r-- 1 0 0 54 Nov 21 2002 sed-4.0.2.README -rw-r--r-- 1 0 0 149149 Dec 12 2002 sed-4.0.3-4.0.4.diff.gz -rw-r--r-- 1 0 0 89364 Mar 18 2003 sed-4.0.5-4.0.6.diff.gz -rw-r--r-- 1 0 0 688434 Mar 18 2003 sed-4.0.6.tar.gz -rw-r--r-- 1 0 0 27912 Apr 11 2003 sed-4.0.6-4.0.7.diff.gz -rw-r--r-- 1 0 0 693903 Apr 11 2003 sed-4.0.7.tar.gz lrwxrwxrwx 1 0 0 19 Aug 02 2003 sed-4.0.1.tar.gz.back-RSN.README -> ../../MISSING-FILES lrwxrwxrwx 1 0 0 19 Aug 02 2003 sed-4.0.3.tar.gz.back-RSN.README -> ../../MISSING-FILES lrwxrwxrwx 1 0 0 19 Aug 02 2003 sed-4.0.4-4.0.5.diff.gz.back-RSN.README -> ../../MISSING-FILES lrwxrwxrwx 1 0 0 19 Aug 02 2003 sed-4.0.4.tar.gz.back-RSN.README -> ../../MISSING-FILES lrwxrwxrwx 1 0 0 19 Aug 02 2003 sed-4.0.5.tar.gz.back-RSN.README -> ../../MISSING-FILES lrwxrwxrwx 1 0 0 19 Aug 02 2003 sed-4.0.tar.gz.back-RSN.README -> ../../MISSING-FILES -rw-r--r-- 1 0 0 734588 Oct 21 2003 sed-4.0.8.tar.gz -rw-r--r-- 1 0 0 65 Oct 21 2003 sed-4.0.8.tar.gz.sig -rw-r--r-- 1 1003 65534 65 Jan 10 2004 sed-4.0.9.tar.gz.sig -rw-r--r-- 1 1003 65534 769849 Jan 10 2004 sed-4.0.9.tar.gz -rw-r--r-- 1 1003 65534 773204 Jun 15 2004 sed-4.1.tar.gz -rw-r--r-- 1 1003 65534 65 Jun 15 2004 sed-4.1.tar.gz.sig -rw-r--r-- 1 1003 65534 771356 Jul 06 2004 sed-4.1.1.tar.gz -rw-r--r-- 1 1003 65534 64 Jul 06 2004 sed-4.1.1.tar.gz.sig -rw-r--r-- 1 1003 65534 65 Jul 06 2004 sed-4.1-4.1.1.diff.gz.sig -rw-r--r-- 1 1003 65534 35595 Jul 06 2004 sed-4.1-4.1.1.diff.gz -rw-r--r-- 1 1003 65534 767189 Aug 22 2004 sed-4.1.2.tar.gz -rw-r--r-- 1 1003 65534 65 Aug 22 2004 sed-4.1.2.tar.gz.sig -rw-r--r-- 1 1003 65534 65 Jan 19 09:36 sed-4.1.3.tar.gz.sig -rw-r--r-- 1 1003 65534 793673 Jan 19 09:36 sed-4.1.3.tar.gz -rw-r--r-- 1 1003 65534 794257 Jan 28 09:17 sed-4.1.4.tar.gz -rw-r--r-- 1 1003 65534 65 Jan 28 09:18 sed-4.1.4.tar.gz.sig |
Posted by windtear at May 31, 2005 8:31 PM