關於mac OSX下的sed

mac OS X下的sed出錯

      其實這裏只是記錄一下自己在用sed修改文件的時候遇到的現象。

      網上有很多sed的講解,還算詳細。今天打算改一些網頁,打算加一句代碼,於是有了如下的語句:

sed -i "s/class=\"look\"/class=\"look\" id=\"lookBigView\"/g" *

      然而報錯:

sed: -i may not be used with stdin

      = 。=|||
      我覺得自己循規蹈矩,按照步驟一點點來的,什麼叫不能用標準輸入……我沒有用啊!!!然後利用國內某知名搜索引擎一下午……無解……面對這樣的腳本問題,一句話的事情,作爲一名有志氣的程序員,怎能咽得下這口氣!於是轉戰Google,好吧,瞬間把我拽到的Stackoverflow(原問題點我

The problem is that Mac OS X uses the BSD version of sed, which treats the -i option slightly differently. The GNU version used in Linux takes an optional argument with -i: if present, sed makes a backup file whose name consists of the input file plus the argument. Without an argument, sed simply modifies the input file without saving a backup of the original.

In BSD sed, the argument to -i is required. To avoid making a backup, you need to provide a zero-length argument, e.g. sed -i ” y.tab.c ….

Your command, which simply edits y.tab.c with no backup in Linux, would attempt to save a backup file using ‘y.tab.c’ as an extension. But now, with no other file in the command line, sed thinks you want to edit standard input in-place, something that is not allowed.

      終於明白不是我的錯了(T 0 T)…Mac OS X是BSD的孩子,那麼就跟着父親來,sed -i 之後需要加參數,表示備份原文件……

      於是,代碼如下即可:

sed -i "" "s/class=\"look\"/class=\"look\" id=\"lookBigView\"/g" *
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章