linux shell 之 sed
[ftpuser@b2cbbstest ~]$ whereis sed
sed: /bin/sed /usr/share/man/man1p/sed.1p.gz /usr/share/man/man1/sed.1.gz
[ftpuser@b2cbbstest ~]$ which sed
/bin/sed
[ftpuser@b2cbbstest ~]$ sed --version
GNU sed version 4.1.5
Copyright (C) 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.
[ftpuser@b2cbbstest ~]$ sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
-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)
-c, --copy
use copy instead of rename when shuffling files in -i mode
(avoids change of input file ownership)
-l N, --line-length=N
specify the desired line-wrap length for the `l' command
--posix
disable all GNU extensions.
-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
--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.
crosoft Windows [???? 6.1.7601]
???????? (c) 2009 Microsoft Corporation????????????????
C:\Users\Administrator>cd D:\soft\UnxUtils\bin
C:\Users\Administrator>d:
D:\soft\UnxUtils\bin>ls
sh.exe
D:\soft\UnxUtils\bin>sh.exe
$P$G?
zsh: no matches found: ?
$P$Ghelp
abnormal program termination
zsh: fork failed: no such file or directory
$P$G
# 在每一行后面增加一空行
sed G
# 将原来的所有空行删除并在每一行后面增加一空行。
# 这样在输出的文本中每一行后面将有且只有一空行。
sed '/^$/d;G'
# 在每一行后面增加两行空行
sed 'G;G'
# 将第一个脚本所产生的所有空行删除(即删除所有偶数行)
sed 'n;d'
# 在匹配式样“regex”的行之前插入一空行
sed '/regex/{x;p;x;}'
# 在匹配式样“regex”的行之后插入一空行
sed '/regex/G'
# 在匹配式样“regex”的行之前和之后各插入一空行
sed '/regex/{x;p;x;G;}'
批量修改当前文件夹下所有html文件里的特定字符串"111"为"one" 字符串
[iyunv@localhost webapps]# sed -i 's/one/111/' *.html
批量修改当前文件夹下和子文件夹下所有html文件里的特定字符串"111"为"one"字符串
[iyunv@localhost webapps]# grep -r -l 111 * | xargs sed -i 's/111/one/g'
或
[iyunv@localhost webapps]# grep -r -l one */*.html | xargs sed -i 's/111/one/g'