如何在bash中复制或删除特定行并将其创建为新文件

时间:2015-12-02 18:57:42

标签: linux bash delete-row

我有一个auto_generated文件可能有重复的数据,这会导致我的解析器崩溃。如何逐行检查并根据bash上的字符删除不需要的行?例如:

   for line in file.txt:
            if '(1)' in line:
                delete line
            elif '(2)' in line:
                delete line
            elif '(3)' in line:
                delete line
            else:
                return (file.txt with those lines removed )

示例输入

Hello my name is john
Hello my name is eric
Hello my name is jonh(2)
Hello my name is ray
Hello my name is john (1)
Hello my name is eric (3)

示例输出

Hello my name is john
Hello my name is eric
Hello my name is ray

1 个答案:

答案 0 :(得分:1)

要排除模式( +字母+ )的行,您可以这样做:

grep -v '(.)' file

如果您希望这封信是一个数字:

grep -v '([0-9])' file

如果您想排除一个特定的号码:

grep -v '(1)' file

如果要排除多个特定数字:

grep -v '([123])' file

如果要排除多个不同的模式

grep -v -e pattern1 -e pattern2 -e pattern3 file