s2p-Sed to Perl translator
(a) 使用linux redhat 7.0,s2p所在的位置是/usr/bin/s2p
(b) 使用 ldd /usr/bin/s2p 後顯示 : "not a dynamic executable"
(c) 和awk一樣,perl在語意上也是awk的超集合,所以我們也可以用轉換程式s2p把sed� {式轉成perl。s2p的用法和a2p一模一樣,不過s2p轉換時几乎不會出錯,所以可以放心使用。執行轉換� 嶊熊{式時,可以用或不用-n選項,意義和sed的同名選項一樣。如果加上-n,轉換過的� {式會先送給c的前置處理器(preprocessor),所以開始時會稍微慢一點。如果可事先得知呼叫此� {式時不會用到 -n選項,可以在用s2p轉換時加-n和-p這二個參數,通知s2p進行最嘉化。
(d) 參數的意義
用法:s2p [options] filename
- D
設立除錯旗幟
- n
這個sed script是和sed -n 關連的,sed的用法請查man sed。
-p
這個sed script是和sed -n 無關的。
例子:
| (1)sed -n '/sentimental/p'filex | |
| 在螢幕上印出所有包含sentimental的行。若沒有-n選項,所有包含sentimental的行都會顯 | 示兩次。filex不會被改變。 |
(2)sed -n '15,20p'filex
會顯示第15到20行。
(e) -d
是用debug用的
-n 主要是可以針對某個特定的樣式來做處理。
(f) 假設有一檔案data的內容如下:
####################################################################
# northwest NW CharlesMain 3.0 .98 3 34 #
# western WE Sharon Gray 5.3 .97 5 23 #
# southwest SW Lewis Dalsass 2.7 .8 2 18 #
####################################################################
而另一個可執行檔b如下:
| ##################################################################### | |
| # | /eastern/,/southeast/{ # |
| # | /^*$/d # |
| # | /Suan/{h;d;} # |
|
|
|
| # | /Ann/g # |
| # | s/TB\(Savage\)/thomas\1/ # |
| ##################################################################### | |
| 可執行檔b會對檔案data做處理,現在我下了一個s2p b的指令,過後會顯示轉成perl的結果: | |
| ##################################################################### | |
| # | #!/usr/bin/perl # |
| # | eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' # |
| # | if $running_under_some_shell; # |
|
|
|
| # | while ($ARGV[0] =~ /^-/) { # |
| # | $_ = shift; # |
| # | last if /^--/; # |
| # | if (/^-n/) { # |
| # | $nflag++; # |
| # | next; # |
|
|
|
| # | die "I don't recognize this switch: $_\\n"; # |
|
|
|
| # | $printit++ unless $nflag; # |
|
|
|
| # | $\ = "\n"; # automatically add newline on print # |
|
|
|
| # | LINE: # |
| # | while (<>) { # |
| # | chop; # |
| # | if (/eastern/.../southeast/) { # |
| # | if (/^*$/) { # |
| # | $printit = 0; # |
| # | next LINE; # |
|
|
|
| # | if (/Suan/) { # |
| # | $hold = $_; # |
|
|
|
| # | $_ = $hold if /Ann/; # |
| # | s/TB(Savage)/thomas$1/; # |
|
|
|
| # | continue { # |
| # | if ($printit) # |
| # | { print; } # |
|
|
|
| # | { $printit++ unless $nflag; } # |
|
|
|
| ##################################################################### | |
| 轉換成perl程式儲存以後,要用chmod +x 來使perl程式可以執行,效果跟原本的程式一樣。如果轉換以後的perl程式無法執行,那就要自已debug了。 | |