加入收藏 | 设为首页 | 会员中心 | 我要投稿 晋中站长网 (https://www.0354zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Sed - An Introduction and Tutorial by Bruce Barnett

发布时间:2021-01-25 14:11:33 所属栏目:Linux 来源:网络整理
导读: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#uh-10a"gt;

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/#.*//'

The first pattern turns on a flag that tells?sed?to perform the substitute command on every line. The second pattern turns off the flag. If the "start" and "stop" pattern occurs twice,the substitution is done both times. If the "stop" pattern is missing,the flag is never turned off,and the substitution will be performed on every line until the end of the file.

You should know that if the "start" pattern is found,the substitution occurs on the same line that contains "start." This turns on a switch,which is line oriented. That is,the next line is read and the substitute command is checked. If it contains "stop" the switch is turned off. Switches are line oriented,and not word oriented.

You can combine line numbers and regular expressions. This example will remove comments from the beginning of the file until it finds the keyword "start:"

sed -e '1,/start/ s/#.*//'

This example will remove comments everywhere except the lines?between?the two keywords:

sed -e '1,/start/ s/#.*//' -e '/stop/,$ s/#.*//'

The last example has a range that overlaps the "/start/,/stop/" range,as both ranges operate on the lines that contain the keywords. I will show you later how to restrict a command up to,?but not including?the line containing the specified pattern. It is in??But I have to cover some more basic principles.

Before I start discussing the various commands,I should explain that some commands cannot operate on a range of lines. I will let you know when I mention the commands. In this next section I will describe three commands,one of which cannot operate on a range.

Using ranges can be confusing,so you should expect to do some experimentation when you are trying out a new script. A useful command deletes every line that matches the restriction: "d." If you want to look at the first 10 lines of a file,you can use:

sed '11,$ d' 

which is similar in function to the?head?command. If you want to chop off the header of a mail message,which is everything up to the first blank line,use:

sed '1,/^$/ d' 

You can duplicate the function of the?tail?command,assuming you know the length of a file.?Wc?can count the lines,and?expr?can subtract 10 from the number of lines. A Bourne shell script to look at the last 10 lines of a file might look like this:?#!/bin/sh#print last 10 lines of file# First argument is the filenamelines=`wc -l $1 | awk '{print $1}' `start=`expr $lines - 10`sed "1,$start d" $1

Click here to get file:?The range for deletions can be regular expressions pairs to mark the begin and end of the operation. Or it can be a single regular expression. Deleting all lines that start with a "#" is easy:

sed '/^#/ d'

(编辑:晋中站长网)

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

热点阅读