2014-12-03 225 views
1

我想寫一個腳本來自動生成具有不同參數的多個輸出文件的過程。這需要在以下代碼語句中的CONFIG.c_mm2s_burst_size和CONFIG.c_s2mm_burst_size之後替換花括號中的數字。用正則表達式替換大括號內的數字

set_property -dict [ list CONFIG.c_include_mm2s {1} CONFIG.c_include_mm2s_dre {0} CONFIG.c_include_s2mm_dre {0} CONFIG.c_include_sg {0} CONFIG.c_m_axi_mm2s_data_width {32} CONFIG.c_m_axis_mm2s_tdata_width {32} CONFIG.c_micro_dma {0} CONFIG.c_mm2s_burst_size {2} CONFIG.c_s2mm_burst_size {2} CONFIG.c_sg_length_width {23} ] $axi_dma_0 

該代碼在tcl。我試過變量替換,但它不能正確解釋類似的東西 CONFIG.c_mm2s_burst_size {$var}

所以我認爲用sed和perl替換文本中的數字應該不難。但是,我搜索了整整一晚都沒有成功。

我曾嘗試:

sed -r 's/burst_size\>\s\{(\d+)\}/256/g' 

sed -r 's/burst_size\s\{(\.+)\}/256/g' 

sed -r 's/burst_size#\{(\d+)\}/256/g' 

sed -r 's/burst_size\s\\{(\d+)\\}/256/g' 

更多的人,他們沒有工作。我在GNU 4.2.2上使用Ubuntu。只要我係統地更改數字,歡迎使用其他語言的其他語言。

非常感謝

回答

1

要更換裏面這是之前由字符串burst_size{}括號中的數字partcular,你可以使用下面的sed命令。 sed將不支持\s\d。取而代之的\s您可以使用POSIX符號的[[:space:]],而是\d,你可以使用[0-9]

sed 's/\(burst_size \+{\)[0-9]\+}/\1256}/g' 
sed -r 's/(burst_size +\{)[0-9]+\}/\1256}/g' 

例子:

$ echo 'CONFIG.c_mm2s_burst_size {2} CONFIG.c_s2mm_burst_size {2} CONFIG.c_sg_length_width {23} ] $axi_dma_0' | sed 's/\(burst_size \+{\)[0-9]\+}/\1256}/g' 
CONFIG.c_mm2s_burst_size {256} CONFIG.c_s2mm_burst_size {256} CONFIG.c_sg_length_width {23} ] $axi_dma_0 
$ echo 'CONFIG.c_mm2s_burst_size {2} CONFIG.c_s2mm_burst_size {2} CONFIG.c_sg_length_width {23} ] $axi_dma_0' | sed -r 's/(burst_size +\{)[0-9]+\}/\1256}/g' 
CONFIG.c_mm2s_burst_size {256} CONFIG.c_s2mm_burst_size {256} CONFIG.c_sg_length_width {23} ] $axi_dma_0 
+0

謝謝Raj!這樣可行。 – user2404894 2014-12-03 05:33:07

+0

不客氣.. – 2014-12-03 05:34:07

1

您可以簡單地使用下面的Perl一行代碼。

perl -pe 's/burst_size\s+{\K\d+/256/g' 
+0

謝謝HWND,工程。但是,如果我接受第一響應者的答案,希望你不介意。 – user2404894 2014-12-03 05:29:45

+0

不用擔心.... – hwnd 2014-12-03 05:30:18

1

\s\d而不是sed的

認識你可以使用

sed -r 's/(burst_size)\{[0-9]+\}/\1{256}/g' input 

sed -r 's/(burst_size[[:space:]])\{[[:digit:]]+\}/\1{256}/g' input 
+0

非常感謝您的幫助! – user2404894 2014-12-03 05:32:49

+0

@ user2404894歡迎您:) – nu11p01n73R 2014-12-03 05:35:15

相關問題