Linux / Bash - 在每两次出现的“^ [[”字符

时间:2017-05-08 04:52:33

标签: linux bash awk sed

我有一个包含以下内容的文件(以及行号):

      1 Started by user ^[[8mha:AAAAmx+LCAAAAAAAAP9b85aBtbiIQTGjNKU4P08vOT+vOD8nVc83PyU1x6OyILUoJzMv2y+/JJUBAhiZGBgqihhk0NSjKDWzXb3RdlLBUSYGJk8GtpzUvPSSDB8G5tKinBIGIZ+sxLJ        E/ZzEvHT94JKizLx0a6BxUmjGOUNodHsLgAzeEgZ+/dLi1CL9jMSi4ozMvMxsAJ8wnzvEAAAA^[[Blahblah
      2 [abc] - Loading variables.
      3 Building on master 
      4 [test] 
      5 Started job
      6 Build log at - /xyz/file
      7 Ended job
      8 Started by user ^[[8mha:AAAAmx+LCAAAAAAAAP9b85aBtbiIQTGjNKU4P08vOT+vOD8nVc83PyU1x6OyILUoJzMv2y+/JJUBAhiZGBgqihhk0NSjKDWzXb3RdlLBUSYGJk8GtpzUvPSSDB8G5tKinBIGIZ+sxLJ        E/ZzEvHT94JKizLx0a6BxUmjGOUNodHsLgAzeEgZ+/dLi1CL9jMSi4ozMvMxsAJ8wnzvEAAAA^[[computer
      9 Started job

我想删除每两次出现的^[[字符(包括字符)之间的所有文字,这样我的新文件就像这样:

      1 Started by user Blahblah
      2 [abc] - Loading variables.
      3 Building on master 
      4 [test] 
      5 Started job
      6 Build log at - /xyz/file
      7 Ended job
      8 Started by user computer
      9 Started job

我怎么能在Bash中这样做?

2 个答案:

答案 0 :(得分:2)

假设线路上只有两个,你可以使用:

sed 's/\^\[\[.*\^\[\[//'

根据以下成绩单:

pax> echo '1 Started by user ^[[REMOVE-ME^[[Blahblah
...> 2 [abc] - Loading variables.
...> 3 Building on master
...> 4 [test]
...> 5 Started job
...> 6 Build log at - /xyz/file
...> 7 Ended job
...> 8 Started by user ^[[REMOVE-ME-AS-WELL^[[computer
...> 9 Started job' | sed 's/\^\[\[.*\^\[\[//'

1 Started by user Blahblah
2 [abc] - Loading variables.
3 Building on master 
4 [test] 
5 Started job
6 Build log at - /xyz/file
7 Ended job
8 Started by user computer
9 Started job

sed命令只是简单地替换第一个和最后一个^[[序列之间的所有内容,其中\个字符只是为了逃避{{{ 1}}正则表达式引擎。

要注意的一点是,如果分隔符与您描述的完全相同,那将会起作用。我只提到这一点,因为sed有时是^[字符的表示。如果 ,则需要调整正则表达式以适应:

ESC

答案 1 :(得分:1)

我建议:

sed 's/\(\^\[\[\).*\1//' file

如果您要“就地”修改文件,请使用sed的选项-i