`

OS + Linux Shell Sed / Linux Sed / linux Sed / Linux sed / linux sed / sed

    博客分类:
  • OS
 
阅读更多

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.

 

sed对于段落的处理.

http://www.linuxsir.org/bbs/thread165324.html

test.txt

a~1234
xxxxx
bbbbb
ooooo
DONE

a~5678
xxxxxx
ffffffff
kkkkkkk
mmmmmmm
NONDONE

a~112334
kdjfiels
dfjldkdeee
dkccke
skdjfieiw
11
DONE

a~11234
pppooeif
kkkdekde
gnome
helloworld
hihihihi
ohohohoh
DONE

a~!3324
ksdjofe-
lskdjfi,skddjie
killid
NONDONE

a~3325
knoppix
gnoppix
santa fa
quake3d
goodgood
successful!!!
DONE

  要求:输出结果: 只显示"NONDONE"的段落块或"DONE"的段落块.

[ftpuser@b2cbbstest ~]$sed -n '/\<DONE\>/{n;h;b};/\<NONDONE\>/{H;x;p;n;x;b};H'   test.txt

 

// 这个将输出有NODONE的字段
[ftpuser@b2cbbstest ~]$ sed ':a;N;/DONE$/!ba;/\nDONE/d'   test.txt 
//  这个将输出只有DONE的字段
[ftpuser@b2cbbstest ~]$ sed ':a;N;/DONE$/!ba;/\nNONDONE/d' test.txt

 

 

http://hnsswjjxy-163-com.iteye.com/blog/910018

sed编辑器逐行处理输入,然后把结果发送到屏幕。

 

 

sed命令和选项:

 

a\ 在当前行后添加一行或多行
c\ 用新文本替换当前行中的文本
d 删除行
i\ 在当前行之前插入文本
h 把模式空间的内容复制到暂存缓冲区
H 把模式空间的内容添加到缓冲区
g 取出暂存缓冲区的内容,将其复制到模式缓冲区
G 取出暂存缓冲区的内容,将其追加到模式缓冲区
l 列出非打印字符
p 打印行
n 读入下一行输入,并从下一条而不是第一条命令对其处理
q 结束或退出sed
r 从文件中读取输入行
! 对所选行以外的行应用所有命令
s 用一个字符串替换另外一个字符串

 

 

 

替换标志:

 

g 在行内进行全局替换
p 打印行
w 将行写入文件
x 交换暂存缓冲区和模式空间的内容
y 将字符转换成另外一个字符

 

 

sed例子:

 

打印:p命令

 

sed ‘/abc/p’ file

打印file中包含abc的行。默认情况sed把所有行都打印到屏幕,如果某行匹配到模式,则把该行另外再打印一遍

sed  -n ‘/abc/p’ file 和上面一样,只是去掉了sed的默认行为,只会打印匹配的行

 

 

删除:d命令

 

sed ‘3,$d’ file    删除从第3行到最后一行的内容。
sed ‘$d’ file 删除最后一行的内容
sed ‘/abc/d’ 删除包含abc的行。
sed ‘3d’ file 删除第三行的内容

 

 

替换:s命令

 

sed  ‘s/abc/def/g’ file 把行内的所有abc替换成def,如果没有g,则只替换行内的第一个abc
sed  -n ‘s/abc/def/p’ file 只打印发生替换的那些行
sed  ‘s/abc/&def/’ file 在所有的abc后面添加def(&表示匹配的内容)
sed  -n ‘s/abc/def/gp’ file 把所有的abc替换成def,并打印发生替换的那些行
sed  ‘s#abc#def#g’ file 把所有的abc替换成def,跟在替换s后面的字符就是查找串和
替换串之间的分割字符,本例中试#

 

 

指定行的范围:逗号

 

sed  -n ‘/abc/,/def/p’ file 打印模式abc到def的行
sed  -n ‘5/,/def/p’ file 打印从第五行到包含def行之间的行。
sed /abd/,/def/s/aaa/bbb/g 修改从模式abc到模式def之间的行,把aaa替换成def

 

 

多重编辑-e

 

sed  -e ‘1,3d’ -e ‘s/abc/def/g’ file 删除1-3行,然后把其余行的abc替换成def

 

 

读文件:r命令

 

sed  ‘/abc/r newfile’ file 在包含abc的行后读入newfile的内容

 

 

写文件:w命令   

 

sed  ‘/abc/w newfile’ file 在包含abc的行写入newfile

 

 

追加:a命令      

 

sed  ‘/abc/a\def’ file 在包含abc的行后新起一行,写入def

 

 

插入:i命令      

 

sed  ‘/abc/i\def’ file 在包含abc的行前新起一行,写入def

 

 

修改:c命令    

 

sed  ‘/abc/c\def’ file 在包含abc的行替换成def,旧文本被覆盖

 

 

读取下一行:n命令   

 

sed  ‘/abc/{n ; s/aaa/bbb/g;}’ file 读取包含abc的行的下一行,替换aaa为bbb

 

 

转换:y命令        

 

sed  ‘y/abc/ABC’ file 将a替换成A,b替换成B,c替换成C(正则表达式元字符不起作用)

 

 

退出:q命令    

 

sed  ‘/abc/{ s/aaa/bbb/ ;q; }’ file 在某行包含了abc,把aaa替换成bbb,然后退出sed。

 

 

暂存和取用:h命令(把模式行存储到暂存缓冲区)和g(取出暂存缓冲区的行并覆盖模式缓冲区)G(取出临时缓冲区的行)命令  

 

h和g是复制行为(覆盖),H和G表示追加。    

sed  -e ‘/abc/h’  -e ‘$G’ file 包含abc的行通过h命令保存到暂存缓冲区,在第二条命令汇中,sed读到最后一行$时,G命令从暂存缓冲区中读取一行,追加到模式缓冲区的后面。即所有包含abc的行的最后一行被复制到文件末尾。
sed -e ‘/abc/{h; d;}’
      -e  ‘/def/{g; }’ file
包含abc的行会移到包含def的行上,并进行覆盖。

 

 

暂存和互换:h和x命令     

 

sed  -e ‘/abc/h’ 
-e ‘/def/x’ file
包含abc的行会被换成def的行。

 

 

-i选项:直接作用源文件,源文件将被修改。

 

文件:test.txt  要求用sed方法,过滤单个文件里的所有空白行

http://zhidao.baidu.com/question/82721866.html?fr=qrl&index=0

内容:

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


tt

解决方法1:

[root@localhost ~]# sed '/^$/d'  test.txt  > test2.txt

[root@localhost ~]# sed “/^$/d”  test.txt  > test2.txt

解决方法2:

UltraEdit-32  删除单个文件所有空白行、空行,使文字紧凑排版

解决:

http://zhidao.baidu.com/question/48144939.html

Ctrl + R 输入:^p^p 替换为^p 即可

如是UNIX格式,替换 ^n^n为^n 
如是DOS格式,替换^r^n^r^n为^r^n 可多次替换达到最终效果。 
尝试^r^n^p的多种组合,将两个序列替换成一个序列。

 

 

# 在每一行后面增加一空行
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" 字符串
[root@localhost webapps]# sed -i 's/one/111/' *.html
批量修改当前文件夹下和子文件夹下所有html文件里的特定字符串"111"为"one"字符串
[root@localhost webapps]# grep -r -l 111 * | xargs sed -i 's/111/one/g'

[root@localhost webapps]# grep -r -l one */*.html | xargs sed -i 's/111/one/g'
批量修改当前文件夹下和子文件夹下所有html文件里的特定字符串"111"为"one"字符串,
包含带特殊字符转义的写法如下:
[root@localhost webapps]# grep -r -l "href=\"http:\/\/www.shopxx.net\" target=\"_blank\">SHOP<span>++<\/span> V2.0" * | xargs -I {} sed -i 's/href=\"http:\/\/www.shopxx.net\" target=\"_blank\">SHOP<span>++<\/span> V2.0/href=\"http:\/\/Lindows.ITeye.Com\" target=\"_blank\">Lindows<span>++<\/span> V2.0/g' {}
[root@localhost admin]# grep -r -l 'Lindows.ITeye.Com' *
admin_js.ftl
page_header.ftl
page_index.ftl
[root@localhost webapps]# grep -r -l "www.shopxx.com" *|xargs -I {} sed -i 's/www.shopxx.com/Lindows.ITeye.Com/g' {}
end
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics