加入收藏 | 设为首页 | 会员中心 | 我要投稿 网站开发网_马鞍山站长网 (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

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'

Removing comments and blank lines takes two commands. The first removes every character from the "#" to the end of the line,and the second deletes all blank lines:

sed -e 's/#.*//' -e '/^$/ d'

A third one should be added to remove all blanks and tabs immediately before the end of line:

sed -e 's/#.*//' -e 's/[ ^I]*$//' -e '/^$/ d' 

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

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