2014-11-05 132 views
1

嘗試刪除txt文件中兩個字符串(/**/)之間的文本而不刪除全行。刪除unix中兩個單詞之間的字符串

可以說我有一個包含下列文件:

/* reports */ 
%report1([email protected] /*[email protected] */,lag=3); 
%report2([email protected] /*[email protected] 
[email protected] */ ,lag=3); 

我所需的輸出是

%report1([email protected], lag=3); 
%report2([email protected] 
,lag=3); 

我已經試過的trsed,並awk多種組合,但仍無法正常工作。 有什麼想法?請注意,報告2分爲兩行,分隔線分別爲/**/

+0

你想最後'滯後= 3'to是'%report2'落後? – Jotne 2014-11-06 07:00:41

回答

1

隨着:

perl -0777pe '[email protected]/\*.*?\*/@@gs' file.txt 
+1

這工作完全正確....謝謝先生 – 2014-11-05 22:39:54

+0

不客氣=) – 2014-11-05 22:40:26

+1

@sputnick如此醜陋,但仍然如此美麗! – 2014-11-05 22:51:21

0
awk 'BEGIN { RS="/"; ORS="" }\ 
    /^\*/,/\*$/ { f=1; next }\ 
    { if (f!=0) f=0; else print "/"; print }' file.txt 

說明

BEGIN { RS="/"; ORS="" }設置記錄隔板"/",並輸出記錄分隔符爲空字符串。

/^\*/,/\*$/ { f=1; next }跳過一個與*開始,一個與*結束之間的任何記錄,如果標誌f設置,那麼取消設置它,而設置標誌f爲1

{ if (f!=0) f=0; else print "/"; print },否則打印/,始終打印記錄。

This does not處理髮生在字符串中的/**/的情況。

相關問題