2

$ cat thegeekstuff.txt

#Linux
        Administration
        Scripting
        Tips and Tricks

#Windows
        Administration

#Database
        Mysql
        Mysql
        Oracle
        Queries
        Mysql
        Procedures

$ sed -n '/Mysql/{g;1!p;};h' thegeekstuff.tx

#Database
#Database
        Queries

これは、パターンに一致する行に対して h コマンドが実行されないことを意味します。しかし、私の印象では、アドレス選択のないコマンドはすべての行に適用されます。誰かがなぜこのように振る舞うのか説明できますか?

4

2 に答える 2

3

GNU のコード: 説明

sed -n '/Mysql/{g;1!p;};h'

h  # copy pattern space to hold space

/Mysql/{ # commands if the first pattern /Mysql/ is found
    g  # copy hold space to pattern space, in first /Mysql/ "#Database" is in hold space from the last line "h" command
    1!p # print the pattern space except in line #1, "#Database" is printed
 h # copy pattern space "#Database" to hold space

 /Mysql/{ # commands if the second pattern /Mysql/ is found
    g  # copy hold space to pattern space, in second /Mysql/ "#Database" is again in hold space from last "h" command
    1!p # print the pattern space except in line #1, "#Database" is printed again
  h # copy pattern space "#Database" to hold space

 /Mysql/{ # commands if the third pattern /Mysql/ is found
    g  # copy hold space to pattern space, in third /Mysql/ is "Queries" in hold space from last line "h" command
    1!p # print the pattern space except in line #1, "Query" is printed now
  h # copy pattern space to hold space
} end program
于 2013-07-04T17:01:49.000 に答える