How to copy or delete specific lines in bash and create them into a new file
本问题已经有最佳答案,请猛点这里访问。
我有一个auto_generated文件,可能有重复的数据,这会导致我的解析器崩溃。 如何逐行检查并根据bash上的字符删除不需要的行? 例如:
1 2 3 4 5 6 7 8 9 | 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 ) |
样本输入
1 2 3 4 5 6 | 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) |
样本输出
1 2 3 | Hello my name is john Hello my name is eric Hello my name is ray |
要排除具有模式
1 | grep -v '(.)' file |
如果你想要这封信是一个数字:
1 | grep -v '([0-9])' file |
如果要排除单个特定数字:
1 | grep -v '(1)' file |
如果要排除多个特定数字:
1 | grep -v '([123])' file |
如果要排除多个不同的模式:
1 | grep -v -e pattern1 -e pattern2 -e pattern3 file |