Sed - An Introduction and Tutorial by Bruce Barnett
|
Click here to get file:?You can modify this to print any number of lines around a pattern. As you can see,you must remember what is in the hold space,and what is in the pattern space. There are other ways to write the same routine. Instead of exchanging the hold space with the pattern space,you can copy the hold space to the pattern space with the "g" command. This deletes the pattern space. If you want to append to the pattern space,use the "G" command. This adds a new line to the pattern space,and copies the hold space after the new line. Here is another version of the "grep3" command. It works just like the previous one,but is implemented differently. This illustrates that?sed?has more than one way to solve many problems. What is important is you understand your problem,and document your solution: #!/bin/sh # grep3 version c: use 'G' instead of H Click here to get file:? The "G" command makes it easy to have two copies of a line. Suppose you wanted to the convert the first hexadecimal number to uppercase,and don't want to use the script I described in an earlier column? #!/bin/sh # change the first hex number to upper case format # uses sed twice # used as a filter # convert2uc Click here to get file:? Here is a solution that does not require two invocations of?sed:? #!/bin/sh # convert2uc version b # change the first hex number to upper case format # uses sed once # used as a filter # convert2uc Click here to get file:?Carl Henrik Lunde suggested a way to make this simpler. I was working too hard.? #!/bin/sh # convert2uc version b # change the first hex number to upper case format # uses sed once # used as a filter # convert2uc Click here to get file:?This example only converts the letters "a" through "f" to upper case. This was chosen to make the script easier to print in these narrow columns. You can easily modify the script to convert all letters to uppercase,or to change the first letter,second word,etc. As you learn about?sed?you realize that it has its own programming language. It is true that it's a very specialized and simple language. What language would be complete without a method of changing the flow control? There are three commands?sed?uses for this. You can specify a label with an text string preceded by a colon. The "b" command branches to the label. The label follows the command. If no label is there,branch to the end of the script. The "t" command is used to test conditions. Before I discuss the "t" command,I will show you an example using the "b" command. This example remembers paragraphs,and if it contains the pattern (specified by an argument),the script prints out the entire paragraph.? #!/bin/sh sed -n ' # if an empty line,check the paragraph /^$/ b para # else add it to the hold buffer H # at end of file,check paragraph $ b para # now branch to end of script b # this is where a paragraph is checked for the pattern :para # return the entire paragraph # into the pattern space x # look for the pattern,if there - print /'$1'/ p ' Click here to get file:? (编辑:上饶站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- nubia z9 plus什么时候上市 nubia z9 plus上市时间
- linux – 如何在bash中将IDN转换为Punycode?
- linux – GRUB stage 1.5的代码驻留在磁盘上的位置是什么?
- 怎样使用lshw检阅Linux设备信息
- 深度刷机让毫秒级一键ROOT成为现实,彻底告别odin刷hboot繁
- linux – 检测我的共享库的两个ABI不兼容版本加载到单个程序
- ruby-on-rails – git post-receive hook没有运行bundle in
- linux – PostgreSQL高性能安装程序
- 如何在Linux上捕获键盘事件并将监视器用作文本显示?
- 三星照片存在哪里 三星如何更改照片存储位置

