2016-11-08 83 views
1

如何使用sed和for循環刪除包含方括號中的模式的行。如何使用sed刪除帶方括號中的模式的行

我在文本文件中有以下幾行。

有了第一個指數從0到6和第2指數從0到3

可以說,我只是想看看[6] [0〜3]。所以使這個

portStateReport[2][0].xxxxx 
portStateReport[2][1].xxxxx 
portStateReport[2][2].xxxxx 
portStateReport[6][0].xxxxx 

是像

portStateReport[6][0].xxxxx 

我想到的是類似以下內容:

for ((i = 0; i < 4; i++)); do 
    sed remove lines with patterns like "[2][i]" data.txt 
done 

我也試圖與倒置的grep

grep -v "\["6]"\["0] data.txt 

但是,我不知道如何去做讓它更具動態性,也可以做[6] [0〜3]。

謝謝。

回答

1

你可以使用grep這個:

grep '\[6\]\[[0-3]\]' file 

portStateReport[6][0].xxxx 
+0

您好我有一個跟進的問題。 – Chanpuru

+0

爲什麼'\ [[5-10] \] \ [[0-3] \]'不起作用。它似乎grep一切,如果我把範圍設置爲[5-10] – Chanpuru

+0

在正則表達式中'5-10'並不意味着'5'到'10'。爲此,您需要'([5-9] | 10)' – anubhava