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:? (编辑:我爱制作网_沈阳站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Linux etcrc.drc.local配置文件用法介绍
- linux – 我的进程如何检测计算机是否正在关闭?
- c – GDB在启动时崩溃(内部错误:follow_die_offset)
- 过年送这个倍儿有面儿!四季沐歌太阳能热水器3680元
- linux-kernel – 绑定驱动程序如何从奴役接口获取RX数据包
- Linux tail命令显示文件结尾的内容
- 10 Linux DIG Command Examples for DNS Lookup--reference
- 唱吧怎么上榜 长沙style恶霸堂客上榜分析
- Jvm原理分析,看了都说好
- linux – 使用10GB内存的Haproxy和50k连接的100%CPU

