sed在指定行上刪除和添加字符

歡迎轉載!轉載時請註明出處:http://blog.csdn.net/nfer_zhuang/article/details/44020599

引言

我在一個項目中需要發佈版本,兩個客戶的需求基本相同,只有是在一個fm34(消迴音)模塊上一個有該功能,另外一個沒有。那麼就存在在發版本的時候根據需要打開和關閉關於fm34相關的代碼。

其中的一個就是是否需要inmod一個ko文件,我的處理是:

  • 在腳本中有inmod該ko的一行代碼,但是默認是被註釋掉的
  • 在給需要該功能的客戶發佈版本時,通過sed將改行的註釋去掉(shell中的#字符)
  • 編譯出帶有fm34功能的版本
  • 發佈版本完成後,記得將該行在註釋回去(有可能在給另外一個客戶發版本)

在這裏,我就需要如何在腳本中自動完成上面的操作,先給出最終的腳本代碼:

#!/bin/bash

# enable snd-soc-wmt-fm34
sed -i '/snd-soc-wmt-fm34/s/^#//' fs_patch/load_drivers.sh

source release_Common.sh

# disable snd-soc-wmt-fm34 back
sed -i '/snd-soc-wmt-fm34/s/^/#&/' fs_patch/load_drivers.sh

上面的代碼主要是包括一下幾個步驟:

  1. 刪除行首的#字符,打開註釋部分代碼
  2. 編譯版本
  3. 將指定行代碼再次註釋起來

sed行首刪除一個字符

sed -i '/snd-soc-wmt-fm34/s/^#//' fs_patch/load_drivers.sh

-i表示在原始文件上進行修改。

       -i[SUFFIX], --in-place[=SUFFIX]
              edit files in place (makes backup if extension supplied)

s/^#//表示將字符串開頭的#字符替換爲空(即去除行首的#字符)

       s/regexp/replacement/
              Attempt  to  match  regexp  against  the  pattern  space.  If successful, replace that portion matched with replacement.  The replacement may contain the special character & to refer to that portion of  the  pattern space  which  matched,  and  the  special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp.

其中s/regexp/replacement/命令支持address ranges,在該腳本中使用的是正則表達式匹配確定address ranges的方式:

/snd-soc-wmt-fm34/表示匹配含有snd-soc-wmt-fm34字符串的行

       /regexp/
              Match lines matching the regular expression regexp.

除了使用/regexp/的方式匹配特定行,sed還支持直接指定行號的方式進行操作,則上面的腳本也可以使用採用下面的方式完成:

sed -i '49s/^#//' fs_patch/load_drivers.sh

       number Match only the specified line number.

注意,上面的數字前後都沒有任何的附加字符。

sed行首添加一個字符

sed -i '/snd-soc-wmt-fm34/s/^/#&/' fs_patch/load_drivers.sh

注意,這裏和上面的刪除操作唯一的不同就在於s/^/#&/部分。其中,^字符匹配行首,#字符是一般字符表示添加該字符,&字符是我們這裏需要重點關心的。在上面的關於s/regexp/replacement/命令描述時有以下字段:

The replacement may contain the special character & to refer to that portion of  the  pattern space  which  matched,  and  the  special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp.

這裏提到了兩種特殊字符:

&:refer to that portion of  the  pattern space  which  matched,即表示前面的正則表達式匹配出來的部分,而在這裏指的就是行首位置。實際上,在此處我們完全可以不要&字符,也是可以完成任務的。

\1...\9:refer to the corresponding matching sub-expressions in the regexp,主要用於多匹配時(如匹配行中的特定位置)分別表示前面的正則表達式匹配出來的部分,這裏的多匹配需要使用()來進行分割,如上面的代碼可以分別使用下面兩種方式進行實現:

sed -i '/snd-soc-wmt-fm34/s/\(^\)/\1#/' fs_patch/load_drivers.sh
sed -i '/snd-soc-wmt-fm34/s/\(^\)\(.*\)/#\2/' fs_patch/load_drivers.sh

具體內容請參考正則表達式相關知識。

其它

本例中是根據工作需要在行首添加和刪除字符,如果是在行尾進行操作,則需要使用&通配符;而如果需要在行中的其它位置進行操作,則可能就需要使用到多匹配的方式。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章