3.2.8.1 打印与否

    -n 选项修改了 sed 的默认行为。当提供此选项时,sed 将不会在操作完成后打印模式空间的最后内容。反之,若在脚本里使用 p,则会明白地将此行显示出来。举例来说,我们可以这样模拟 grep :
    
    sed -n '/<HTML>/p'  *.html        仅显示<HTML>这行
 
    [many@avention test]$ sed -n '/prepare/p' en.txt
    I am going to prepare for my lessons because the Mid-term exam is coming.
    What's more, we should look up new words in the dictionary before class and prepare each lesson carefully before class.
 
    [many@avention test]$ sed -n 's/prepare/PPP/' en.txt                  # -n 选项就不打印任何东西了
    [many@avention test]$
 
    虽然这个例子很简单,但这个功能在复杂的脚本里非常好用。如果你使用一个脚本文件,可通过特殊的首行来打开此功能:
        #n    关闭自动打印     
        /<HTML>/p                        仅打印含<HTML>的行
 
    在 Shell 中,与很多其他 UNIX 脚本式语言一样:# 是注释的意思。sed 注释必须出现在单独的行里,因为它们是语法型命令,意思是:它们是什么事业不做的命令。虽然 POSIX 指出,注释可以放在脚本里的任何位置,但很多旧版 sed 仅允许出现在首行,GNU sed 则无此限制。
 
    [many@avention test]$ sed  's/prepare/PPP/' en.txt  | sed -n '/PPP/p'
    I am going to PPP for my lessons because the Mid-term exam is coming.
    What's more, we should look up new words in the dictionary before class and PPP each lesson carefully before class.
 
   [many@avention test]$ sed -n  -e 's/prepare/PPP/'  -e '/PPP/p'  en.txt
   I am going to PPP for my lessons because the Mid-term exam is coming.
   What's more, we should look up new words in the dictionary before class and PPP each lesson carefully before class.

猜你喜欢

转载自www.cnblogs.com/avention/p/10307156.html