加入收藏 | 设为首页 | 会员中心 | 我要投稿 网站开发网_马鞍山站长网 (https://www.0555zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长百科 > 正文

Sed - An Introduction and Tutorial by Bruce Barnett

发布时间:2021-01-29 10:07:16 所属栏目:站长百科 来源:网络整理
导读:副标题#e# http://www.grymoire.com/unix/sed.html Quick Links - NEW table border="1" tr Sed Pattern Flags /tr tr td a href="http://www.grymoire.com/unix/Sed.html#uh-6"gt;/g?- Global/td /tr tr td a href="http://www.grymoire.com/unix/Sed.html

This illustrates why?sed?scripts get the reputation for obscurity. I could be perverse and show you the example that will search for all lines that start with a "g," and change each "g" on that line to an "s:"

sed '/^g/s/g/s/g'

Adding a space and using an underscore after the substitute command makes this?much?easier to read:

sed '/^g/ s_g_s_g'

Er,I take that back. It's hopeless. There is a lesson here: Use comments liberally in a?sed?script under SunOS. You may have to remove the comments to run the script under a different operating system,but you now know how to write a?sed?script to do that very easily! Comments are a Good Thing. You may have understood the script perfectly when you wrote it. But six months from now it could look like modem noise.

You can specify a range on line numbers by inserting a comma between the numbers. To restrict a substitution to the first 100 lines,you can use:

sed '1,100 s/A/a/'

If you know exactly how many lines are in a file,you can explicitly state that number to perform the substitution on the rest of the file. In this case,assume you used?wc?to find out there are 532 lines in the file:

sed '101,532 s/A/a/'

An easier way is to use the special character "$," which means the last line in the file.

sed '101,$ s/A/a/'

The "$" is one of those conventions that mean "last" in utilities like?cat -e,?vi,and?ed. "cat -e" Line numbers are cumulative if several files are edited. That is,

sed '200,300 s/A/a/' f1 f2 f3 >new

is the same as

cat f1 f2 f3 | sed '200,300 s/A/a/' >new

You can specify two regular expressions as the range. Assuming a "#" starts a comment,you can search for a keyword,remove all comments until you see the second keyword. In this case the two keywords are "start" and "stop:"

sed '/start/,/stop/ s/#.*//'

(编辑:网站开发网_马鞍山站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!