Linux下sed命令的用法介绍

这篇文章主要讲解了“Linux下sed命令的用法介绍”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Linux下sed命令的用法介绍”吧!

这篇文章主要讲解了“Linux下sed命令的用法介绍”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Linux下sed命令的用法介绍”吧!

创新互联长期为数千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为东山企业提供专业的做网站、成都网站建设东山网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。

Linux sed命令详细说明

sed是一种用于过滤和转换文本的流编辑器。用于对输入流(文件或来自管道的输入)执行基本文本转换。

虽然sed在某些方面类似于允许脚本编辑(如ed)的编辑器,但它的工作方式是只传递一次输入,因此效率更高。

思考:

查看当前IP

[root@cjcos01 cjc]# ifconfig

通过ifconfig虽然可以查看IP,但是打印出很多并不关注的信息,如何去掉这部分无用的信息?

可以通过sed加grep实现,方法见后面的示例。

测试数据

[root@cjcos01 cjc]# cat t1.txt 

tao花庵歌tao花坞里tao花庵,tao花庵下tao花仙;tao花仙人种tao树,又摘tao花卖酒钱。酒醒只在花前坐,酒醉还来花下眠;半醒半醉日复日,花落花开年复年。但愿老死花酒间,不愿鞠躬车马前;车尘马足富者趣,酒盏花枝贫者缘。若将富贵比贫贱,一在平地一在天;若将贫贱比车马,他得驱驰我得闲。别人笑我太疯癫,我笑他人看不穿;不见五陵豪杰墓,无花无酒锄作田。

1 打印行

打印第二行 

[root@cjcos01 cjc]# sed -n '2p' /cjc/t1.txt

tao花坞里tao花庵,tao花庵下tao花仙;

打印第2-5行

[root@cjcos01 cjc]# sed -n '2,5p' /cjc/t1.txt

tao花坞里tao花庵,tao花庵下tao花仙;tao花仙人种tao树,又摘tao花卖酒钱。酒醒只在花前坐,酒醉还来花下眠;半醒半醉日复日,花落花开年复年。

打印第10行到结尾行

[root@cjcos01 cjc]# sed -n '10,$p' /cjc/t1.txt

别人笑我太疯癫,我笑他人看不穿;不见五陵豪杰墓,无花无酒锄作田。

打印第2行,第6行,第8,9,10行

[root@cjcos01 cjc]# sed -n '2p;6p;8,10p' /cjc/t1.txt

tao花坞里tao花庵,tao花庵下tao花仙;但愿老死花酒间,不愿鞠躬车马前;若将富贵比贫贱,一在平地一在天;若将贫贱比车马,他得驱驰我得闲。别人笑我太疯癫,我笑他人看不穿;

打印含有tao字的行

[root@cjcos01 cjc]# sed -n '/tao/p' /cjc/t1.txt 

tao花庵歌tao花坞里tao花庵,tao花庵下tao花仙;tao花仙人种tao树,又摘tao花卖酒钱。

打印"酒"字开头的行

[root@cjcos01 cjc]#  sed -n '/^酒/p' /cjc/t1.txt 

酒醒只在花前坐,酒醉还来花下眠;

打印"。"结尾的行

[root@cjcos01 cjc]#  sed -n '/\。$/p' /cjc/t1.txt 

tao花仙人种tao树,又摘tao花卖酒钱。半醒半醉日复日,花落花开年复年。车尘马足富者趣,酒盏花枝贫者缘。若将贫贱比车马,他得驱驰我得闲。不见五陵豪杰墓,无花无酒锄作田。

2 插入行

[root@cjcos01 cjc]# cp t1.txt t1.txt.bak

人为多愁少年老,花为无愁老少年。年老少年都不管,且将诗酒醉花前。

行前添加,写入源文件

[root@cjcos01 cjc]# sed -i '2i 人为多愁少年老,花为无愁老少年。' /cjc/t1.txt

[root@cjcos01 cjc]# cat t1.txt

tao花庵歌人为多愁少年老,花为无愁老少年。tao花坞里tao花庵,tao花庵下tao花仙;......

行后添加(直接修改原文件)

[root@cjcos01 cjc]# sed -i '2a 年老少年都不管,且将诗酒醉花前。' /cjc/t1.txt

[root@cjcos01 cjc]# cat t1.txt

tao花庵歌人为多愁少年老,花为无愁老少年。年老少年都不管,且将诗酒醉花前。tao花坞里tao花庵,tao花庵下tao花仙;......

3 替换行(直接修改原文件) 

[root@cjcos01 cjc]# sed -i '2c 闲来写就青山卖,不使人间造孽钱。' /cjc/t1.txt

[root@cjcos01 cjc]# cat t1.txt

tao花庵歌闲来写就青山卖,不使人间造孽钱。年老少年都不管,且将诗酒醉花前。......

4 替换字符 

-n 's/old/new/p' 将文件中每行的第一个old字符换成new字符,打印出只发生变化的行,且源文件内容不变

[root@cjcos01 cjc]# sed -n 's/tao/荷/p' /cjc/t1.txt 

荷花庵歌荷花坞里tao花庵,tao花庵下tao花仙;荷花仙人种tao树,又摘tao花卖酒钱。

-n 's/old/new/pg':将文件中全部的old字符换成new字符,打印出只发生变化的行,且源文件内容不变。

[root@cjcos01 cjc]# sed -n 's/tao/荷/pg' /cjc/t1.txt 

荷花庵歌荷花坞里荷花庵,荷花庵下荷花仙;荷花仙人种荷树,又摘荷花卖酒钱。

-n 's/old/new/p3g' :将文件中每行从第3个old字符开始换成new字符,打印出只发生变化的行,且源文件内容不变

[root@cjcos01 cjc]# sed -n 's/tao/荷/p3g' /cjc/t1.txt 

tao花坞里tao花庵,荷花庵下荷花仙;tao花仙人种tao树,又摘荷花卖酒钱。

-i,将文件中每行的第一个old字符换成new字符,修改源文件内容

[root@cjcos01 cjc]# sed -i 's/tao/荷/g' /cjc/t1.txt 

[root@cjcos01 cjc]# sed -i 's/荷/tao/g' /cjc/t1.txt 

5 删除行

删除第2行

[root@cjcos01 cjc]# sed -i '2d' /cjc/t1.txt

删除第3到5行

[root@cjcos01 cjc]# sed -i '3,5d' /cjc/t1.txt

删除第2行,第4,5,6行

[root@cjcos01 cjc]# sed -i '2d;4,6d' /cjc/t1.txt

举例:

例1: 只显示ifconfig中的IP地址

[root@cjcos01 ~]# ifconfig |grep "inet"|grep -v "inet6"|grep -v "127.0.0.1"|grep -v "122.1"|sed 's/netmask.*//'|sed 's/^.*inet//' 192.168.38.10

例2:去掉ssh配置文件中的带#行和空行,不修改源文件,将结果打印到前台

[root@cjcos01 cjc]# echo >t1.txt

[root@cjcos01 cjc]# cat /etc/ssh/ssh_config > t1.txt

[root@cjcos01 cjc]# sed 's/#.*//g' /cjc/t1.txt |sed '/^$/d'

Host *GSSAPIAuthentication yesForwardX11Trusted yesSendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGESSendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENTSendEnv LC_IDENTIFICATION LC_ALL LANGUAGESendEnv XMODIFIERS

例3:每一行结尾为.的换成!("."需要加转义符),不改变源文件(指定-i会改变源文件)

[root@cjcos01 cjc]# sed -n 's/\.$/!/p' /cjc/t1.txt

GSSAPIAuthentication yes!ForwardX11Trusted yes!SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT!

例4:以H开头的行末尾加上@@@

[root@cjcos01 cjc]# sed -n 's/^H.*$/&@@@/p' /cjc/t1.txt 

Host *@@@

sed帮助信息:

[root@cjcos01 ~]# sed --helpUsage: 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  --follow-symlinks                 follow symlinks when processing in place  -i[SUFFIX], --in-place[=SUFFIX]                 edit files in place (makes backup if SUFFIX supplied)  -c, --copy                 use copy instead of rename when shuffling files in -i mode  -b, --binary                 does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (                 open files in binary mode (CR+LFs are not treated specially))  -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  -z, --null-data                 separate lines by NUL characters  --help                 display this help and exit  --version                 output version information and exitIf no -e, --expression, -f, or --file option is given, then the firstnon-option argument is taken as the sed script to interpret.  Allremaining arguments are names of input files; if no input files arespecified, then the standard input is read.GNU sed home page: <http://www.gnu.org/software/sed/>.General help using GNU software: <http://www.gnu.org/gethelp/>.E-mail bug reports to: <bug-sed@gnu.org>.Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.[root@cjcos01 ~]# man sedNAME       sed - stream editor for filtering and transforming textSYNOPSIS       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.......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/sedfaq.txt), 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 command              info sed[root@cjcos01 ~]# info sedFile: sed.info,  Node: Top,  Next: Introduction,  Up: (dir)sed, a stream editor********************This file documents version 4.2.2 of GNU `sed', a stream editor.......

新闻标题:Linux下sed命令的用法介绍
链接地址:https://www.cdcxhl.com/article16/jegg.html

成都网站建设公司_创新互联,为您提供搜索引擎优化静态网站App开发定制开发建站公司网站维护

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站托管运营