Unix替换文件中的特殊字符

时间:2015-01-09 13:16:32

标签: unix sed

我有一个文件如下:

["2015.01.07 10:06:24 UTC","2015.01.07 10:06:34 UTC","2015.01.07 10:06:48 UTC","<a href='/jobhistory/job/job_1418436639844_231528'>job_1418436639844_231528</a>","PigLatin:agg_daily_total_searches_and_unique_searc","data_svc","marathon","SUCCEEDED","1","1","0","0"],
["2015.01.07 10:06:28 UTC","2015.01.07 10:06:35 UTC","2015.01.07 10:13:28 UTC","<a href='/jobhistory/job/job_1418436639844_231529'>job_1418436639844_231529</a>","EndorsementsPendingImpressionEvent-c1201ed1-fb23-4","kafkaetl","marathon","SUCCEEDED","1","1","1","1"],

我需要分别从每行的开头和结尾删除[],

3 个答案:

答案 0 :(得分:3)

您可以尝试以下sed命令。

sed 's/^\[\|\],$//g' file

^断言我们处于一条线的起点,$声称我们处于一条线的末尾。由于基本sed使用BRE(基本正则表达式),因此您需要转义|符号以便像逻辑OR运算符一样运行。

示例:

$ cat ri
["2015.01.07 10:06:24 UTC","2015.01.07 10:06:34 UTC","2015.01.07 10:06:48 UTC","<a href='/jobhistory/job/job_1418436639844_231528'>job_1418436639844_231528</a>","PigLatin:agg_daily_total_searches_and_unique_searc","data_svc","marathon","SUCCEEDED","1","1","0","0"],
["2015.01.07 10:06:28 UTC","2015.01.07 10:06:35 UTC","2015.01.07 10:13:28 UTC","<a href='/jobhistory/job/job_1418436639844_231529'>job_1418436639844_231529</a>","EndorsementsPendingImpressionEvent-c1201ed1-fb23-4","kafkaetl","marathon","SUCCEEDED","1","1","1","1"],
$ sed 's/^\[\|\],$//g' ri
"2015.01.07 10:06:24 UTC","2015.01.07 10:06:34 UTC","2015.01.07 10:06:48 UTC","<a href='/jobhistory/job/job_1418436639844_231528'>job_1418436639844_231528</a>","PigLatin:agg_daily_total_searches_and_unique_searc","data_svc","marathon","SUCCEEDED","1","1","0","0"
"2015.01.07 10:06:28 UTC","2015.01.07 10:06:35 UTC","2015.01.07 10:13:28 UTC","<a href='/jobhistory/job/job_1418436639844_231529'>job_1418436639844_231529</a>","EndorsementsPendingImpressionEvent-c1201ed1-fb23-4","kafkaetl","marathon","SUCCEEDED","1","1","1","1"

答案 1 :(得分:1)

tr -d '[]' < file > new_file

答案 2 :(得分:0)

sed 's/^ *\[\(.*\)] *$/\1/' YourFile

如果没有外部支架保持不变,请将所有内容保留在外部支架内