2017-09-16 62 views
-1

我有一個文件abc.txt,我想從bash腳本中刪除文件中的特定部分。如何使用bash腳本從文件中刪除特定部分

#######################Media###################### 
[Media] 
     comment = Media Files 
     path = /share22 
     browseable = yes 
     read only = yes 
     guest only = no 
#######################end####################### 
#######################Add####################### 
[Add] 
     comment = Media Files 
     path = /share33 
     browseable = yes 
     read only = yes 
     guest only = no 
#######################end####################### 
#######################Added##################### 
[Added] 
     comment = Media Files 
     path = /share44 
     browseable = yes 
     read only = yes 
     guest only = no 
#######################end####################### 

和從文件

#######################Add####################### 
[Add] 
     comment = Media Files 
     path = /share33 
     browseable = yes 
     read only = yes 
     guest only = no 
#######################end####################### 

除去下面部分I如何刪除的第一部分,第二部分或最後部分?

請幫助我!

+0

由文本替換圖像。用四個空格前綴代碼/數據。請看[編輯幫助](http://stackoverflow.com/editing-help)。 – Cyrus

+1

請閱讀http://stackoverflow.com/help/how-to-ask,http://stackoverflow.com/help/dont-ask,http://stackoverflow.com/help/mcve並參加[tour] (http://stackoverflow.com/tour),然後再發布更多Q​​值。祝你好運。 – shellter

回答

0

給定的輸入是適合的簡單的解決方案:

awk '/##Add##/,/##end##/ {next};1' abc.txt 

sed '/##Add##/,/##end##/d' abc.txt 
0

Awk西裝最適合這種類型的文本文件處理,你可以使用下面的一個

awk '/^[#]+(Add)[#]+/{f=1}f && /^[#]+(end)[#]+/{f=0; next}!f' file 

# OR combined one 

awk '/^[#]+(Add|end)[#]+/{if(f){f=0; next}if(/^[#]+(Add)[#]+/)f=1}!f' file 

測試結果:

輸入

$ cat file 
#######################Media###################### 
[Media] 
     comment = Media Files 
     path = /share22 
     browseable = yes 
     read only = yes 
     guest only = no 
#######################end####################### 
#######################Add####################### 
[Add] 
     comment = Media Files 
     path = /share33 
     browseable = yes 
     read only = yes 
     guest only = no 
#######################end####################### 
#######################Added##################### 
[Added] 
     comment = Media Files 
     path = /share44 
     browseable = yes 
     read only = yes 
     guest only = no 
#######################end####################### 

輸出 - 1:

$ awk '/^[#]+(Add)[#]+/{f=1}f && /^[#]+(end)[#]+/{f=0; next}!f' file 
#######################Media###################### 
[Media] 
     comment = Media Files 
     path = /share22 
     browseable = yes 
     read only = yes 
     guest only = no 
#######################end####################### 
#######################Added##################### 
[Added] 
     comment = Media Files 
     path = /share44 
     browseable = yes 
     read only = yes 
     guest only = no 
#######################end####################### 

輸出 - 2:

$ awk '/^[#]+(Add|end)[#]+/{if(f){f=0; next}if(/^[#]+(Add)[#]+/)f=1}!f' file 
#######################Media###################### 
[Media] 
     comment = Media Files 
     path = /share22 
     browseable = yes 
     read only = yes 
     guest only = no 
#######################end####################### 
#######################Added##################### 
[Added] 
     comment = Media Files 
     path = /share44 
     browseable = yes 
     read only = yes 
     guest only = no 
#######################end####################### 
+0

非常感謝我的問題已解決。你能解釋一下命令嗎,因爲我是新手,對我非常有幫助。 –

+0

@ZubairMughal歡迎您,您可以閱讀此https://stackoverflow.com/help/someone-answers –

+0

你能解釋一下嗎? –

相關問題