sed用法

渣渣強發表於2018-07-02

很久沒使用sed命令,今天有一個小需求需要對一批檔案的內容進行修改,於是想起sed命令,順便回顧一下。

文字的內容大致是

item [5]:
		class = "IntervalTier"
		name = "PhonAlign"
		xmin = 0.0
		xmax = 1.799688
		intervals: size = 10
		intervals [1]:
			xmin = 0.0
			xmax = 0.21
			text = "#"
		intervals [2]:
			xmin = 0.21
			xmax = 0.25
			text = "ts"
		intervals [3]:
			xmin = 0.25
			xmax = 0.28
			text = "u"
		intervals [4]:
			xmin = 0.28
			xmax = 0.51
			text = "o"
		intervals [5]:
			xmin = 0.51
			xmax = 0.71
			text = "ts"
		intervals [6]:
			xmin = 0.71
			xmax = 0.74
			text = "u"
		intervals [7]:
			xmin = 0.74
			xmax = 1.02
			text = "o"
		intervals [8]:
			xmin = 1.02
			xmax = 1.23
			text = "ts"
		intervals [9]:
			xmin = 1.23
			xmax = 1.76
			text = "u"
		intervals [10]:
			xmin = 1.76
			xmax = 1.799688
			text = "o"
	item [6]:
		class = "IntervalTier"
		name = "TokensAlign"
		xmin = 0.0
		xmax = 1.799688
		intervals: size = 4
		intervals [1]:
			xmin = 0.0
			xmax = 0.21
			text = "#"
		intervals [2]:
			xmin = 0.21
			xmax = 0.51
			text = "做"
		intervals [3]:
			xmin = 0.51
			xmax = 1.02
			text = "做"
		intervals [4]:
			xmin = 1.02
			xmax = 1.799688
			text = "做"
	item [7]:
		class = "IntervalTier"
		name = "Activity"
		xmin = 0.0
		xmax = 1.799688
		intervals: size = 2
		intervals [1]:
			xmin = 0.0
			xmax = 0.21
			text = "silence"
		intervals [2]:
			xmin = 0.21
			xmax = 1.799688
			text = "speech"
複製程式碼

主要替換的位置是'做'這個次替換成對應的拼音

稍微觀察一下檔案的特點,由於替換的位置位於item[6]~item[7]這兩個字串之間。於是使用

echo "sed -i -E '/item \[6\]/,/item \[7\]/s/text = \"([^#\"]+)\"/text = \"${prex}\"/g'  ${line}"|sh
複製程式碼

這裡有一個小的知識點,使用sed如果直接用變數格式化,但沒辦法進行格式化。於是藉助echo先將格式化好的字串輸出,然後呼叫sh命令執行。

另外還有一個用法,當需要在shell中判斷某個字串的包含關係,可以用如下的條件判斷

if [[ $line =~ "_" ]]
複製程式碼