在命令行sed中替换带有特殊字符的字符串

时间:2016-06-01 16:09:48

标签: linux replace command-line sed special-characters

我想通过在命令行中用x <- seq(0, 10, by = 0.1) y1 <- sin(x) y2 <- sin(x + pi/4) y3 <- cos(x) my.df <- data.frame(time = x, currentA = y1, currentB = y2, voltage = y3) my.df <- melt(my.df, id.vars = "time") my.df$Unit <- as.factor(rep(c("A", "A", "V"), each = length(x))) # Create 3 plots : # A: currentA and currentB plot A = ggplot(my.df, aes(x = time, y = value, color = variable, alpha = variable)) + geom_line() + ylab("A") + scale_alpha_manual(values = c("currentA" = 1, "currentB" = 1, "voltage" = 0)) + guides(alpha = F, color = F) # B: voltage plot B = ggplot(my.df, aes(x = time, y = value, color = variable, alpha = variable)) + geom_line() + ylab("A") + scale_alpha_manual(values = c("currentA" = 0, "currentB" = 0, "voltage" = 1)) + guides(alpha = F, color = F) # C: get the legend C = ggplot(my.df, aes(x = time, y = value, color = variable)) + geom_line() + ylab("A") library(gridExtra) # http://stackoverflow.com/questions/12539348/ggplot-separate-legend-and-plot # use this trick to get the legend as a grob object g_legend<-function(a.gplot){ tmp <- ggplot_gtable(ggplot_build(a.gplot)) leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") legend <- tmp$grobs[[leg]] legend } #extract legend from C plot legend = g_legend(C) #arrange grob (the 2 plots) plots = arrangeGrob(A,B) # arrange the plots and the legend grid.arrange(plots, legend , ncol = 2, widths = c(3/4,1/4)) 替换行%% {some_string, []},来取消注释配置文件的一行。

我使用{some_string, []}尝试了几种不同的格式:

sed

sed 's/%% {some_string, []},/{some_string, []}/' filename

sed "s/%% {some_string, []},/{some_string, []}/" filename

sed "s/'%% {some_string, []},'/'{some_string, []}'/" filename

但每次都收到输出sed 's/"%% {some_string, []},"/"{some_string, []}"/' filename

我假设它与我的格式有关,因为我的特殊字符,但似乎找不到任何其他可能导致问题的相同字符的帖子。我也不能事先将这些字符串声明为变量,因为我需要在命令行中运行它。

1 个答案:

答案 0 :(得分:1)

sed要求您转义方括号。

sed 's/%% {some_string, \[\]},/{some_string, []}/' filename
相关问题