Sed - An Introduction and Tutorial by Bruce Barnett
</tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> |
Occasionally one wishes to use a new line character in a sed script. Well,this has some subtle issues here. If one wants to search for a new line,one has to use "n." Here is an example where you search for a phrase,and delete the new line character after that phrase - joining two lines together.
(echo a;echo x;echo y) | sed '/x$/ { N s:xn:x: }'
which generates
a xy
However,if you are inserting a new line,don't use "n" - instead insert a literal new line character:
(echo a;echo x;echo y) | sed 's:x:X :'
generates
a Xy
So far we have talked about three concepts of?sed: (1) The input stream or data before it is modified,(2) the output stream or data after it has been modified,and (3) the pattern space,or buffer containing characters that can be modified and send to the output stream.
There is one more "location" to be covered: the?hold buffer?or?hold space. Think of it as a spare pattern buffer. It can be used to "copy" or "remember" the data in the pattern space for later. There are five commands that use the hold buffer.
The "x" command eXchanges the pattern space with the hold buffer. By itself,the command isn't useful. Executing the?sed?command
sed 'x'
as a filter adds a blank line in the front,and deletes the last line. It looks like it didn't change the input stream significantly,but the?sed?command is modifying every line.
The hold buffer starts out containing a blank line. When the "x" command modifies the first line,line 1 is saved in the hold buffer,and the blank line takes the place of the first line. The second "x" command exchanges the second line with the hold buffer,which contains the first line. Each subsequent line is exchanged with the preceding line. The last line is placed in the hold buffer,and is not exchanged a second time,so it remains in the hold buffer when the program terminates,and never gets printed. This illustrates that care must be taken when storing data in the hold buffer,because it won't be output unless you explicitly request it.
(编辑:晋中站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!