sed删除除匹配模式以外的所有行

时间:2018-09-05 18:25:28

标签: string search sed

我经历了其他与我相似的问题。 以下命令有效,但是如果我尝试删除除具有字符串“ .c”或“ .h”的行以外的所有行,则该命令不起作用。

sed -r -n -e '/.java|.c/p' test.txt


/home/jenkins/workspace/Test/base/src/packages/.c
/home/jenkins/workspace/Test/base/src/packages/.txt
/home/jenkins/workspace/Test/base/packages/Manager.java

1 个答案:

答案 0 :(得分:2)

使用GNU sed:

List<int> testList = new List<int>{1,76,3,4,5,76,76,8};

public void RemoveElements()
{
    for (int i = testList.Count - 1; i >= 0; i--)
    {
        // RemoveAt option
        if(testList[i] == 76) testList.RemoveAt(i);
    }
}

sed -n '/\.c\|\.h/p' file

sed -n -E '/\.c|\.h/p' file

请参阅:The Stack Overflow Regular Expressions FAQ