2012-06-01 36 views
1

我想用使用shell腳本寫在括號中的數據替換空格。 我的輸入線是如何使用shell腳本在括號內的數據中用「_」替換空格?

2012-05-21六點37分16秒中號NumberOfHwEntitiesMismatch櫃= 1

我希望我的輸出爲(SAU未配置檢測出。):

2012-05-21 6時37分16秒中號NumberOfHwEntitiesMismatch櫃= 1 (SAU_that_is_not_configured_detected。)

請建議我的東西....

回答

1

使用awk,分裂的「(」,然後使用gsub在第二場以取代下劃線的空間。

例子:

$ awk -F\('{gsub(" ","_", $2);print $1"("$2}' <<< "2012-05-21 06:37:16 M NumberOfHwEntitiesMismatch Cabinet=1 (SAU that is not configured detected.)" 
2012-05-21 06:37:16 M NumberOfHwEntitiesMismatch Cabinet=1 (SAU_that_is_not_configured_detected.) 

(這裏假設你的投入只有一組括號)

+0

非常感謝,這是一個很大的幫助 – Anand

+0

嗨羅布麻 請給我解釋一下這個命令 awk -F \('{gsub(「,」_「,$ 2); print $ 1」(「$ 2}' – Anand

+0

首先,'-F \('告訴awk使用'('作爲字段分隔符。 'gsub'函數用第二個字段中的下劃線替換空格字符,最後打印出第一個和第二個字段。 – dogbane

相關問題