在linux中使用sed命令在模式之前和之后就行删除

时间:2019-01-27 21:26:12

标签: shell sed

styles.fab

我想删除模式'name:“。”之前的一行,并删除模式之后的一行(包括模式本身)。我尝试了以下命令,但这并未修改文件本身。我想进行就地修改。请帮助

forward-zone:
name: "."
forward-addr: 10.1.1.2

forward-zone:
name: "mydomain.com"
forward-addr: 10.1.1.1

2 个答案:

答案 0 :(得分:2)

类似的东西可能适合。

sed -rne '/^name: "\."/{n;n;h;d};1{h;d};x;p;${g;p}' -i file

使用-i选项,Sed可以在适当位置修改文件。 Sed命令hgx使用Sed的保持空间来保持前一行,直到检查了当前行。地址1$分别对待文件的第一行和最后一行,这可能需要特殊处理。

尽管这是一项教育活动,但诚然,对于这种多行处理,Awk甚至Perl可能比Sed更好。尽管如此,如您所见,Sed可以做一些额外的工作。

答案 1 :(得分:1)

GNU awk方式:

devtools::install_github("jimhester/lookup")
lookup::lookup(fviz_nbclust)


function (x, FUNcluster = NULL, method = c("silhouette", "wss", 
        "gap_stat"), diss = NULL, k.max = 10, nboot = 100, verbose = interactive(), 
        barfill = "steelblue", barcolor = "steelblue", linecolor = "steelblue", 
        print.summary = TRUE, ...) 
{
        set.seed(123)
        if (k.max < 2) 
                stop("k.max must bet > = 2")
        method = match.arg(method)
        if (!inherits(x, c("data.frame", "matrix")) & !("Best.nc" %in% 
                names(x))) 
                stop("x should be an object of class matrix/data.frame or ", 
                        "an object created by the function NbClust() [NbClust package].")
        if (inherits(x, "list") & "Best.nc" %in% names(x)) {
                best_nc <- x$Best.nc
                if (class(best_nc) == "numeric") 
                        print(best_nc)
                else if (class(best_nc) == "matrix") 
                        .viz_NbClust(x, print.summary, barfill, barcolor)
        }
        else if (is.null(FUNcluster)) 
                stop("The argument FUNcluster is required. ", "Possible values are kmeans, pam, hcut, clara, ...")
        else if (method %in% c("silhouette", "wss")) {
                if (is.data.frame(x)) 
                        x <- as.matrix(x)
                if (is.null(diss)) 
                        diss <- stats::dist(x)
                v <- rep(0, k.max)
                if (method == "silhouette") {
                        for (i in 2:k.max) {
                                clust <- FUNcluster(x, i, ...)
                                v[i] <- .get_ave_sil_width(diss, clust$cluster)
                        }
                }
                else if (method == "wss") {
                        for (i in 1:k.max) {
                                clust <- FUNcluster(x, i, ...)
                                v[i] <- .get_withinSS(diss, clust$cluster)
                        }
                }
                df <- data.frame(clusters = as.factor(1:k.max), y = v)
                ylab <- "Total Within Sum of Square"
                if (method == "silhouette") 
                        ylab <- "Average silhouette width"
                p <- ggpubr::ggline(df, x = "clusters", y = "y", group = 1, 
                        color = linecolor, ylab = ylab, xlab = "Number of clusters k", 
                        main = "Optimal number of clusters")
                if (method == "silhouette") 
                        p <- p + geom_vline(xintercept = which.max(v), linetype = 2, 
                                color = linecolor)
                return(p)
        }
        else if (method == "gap_stat") {
                extra_args <- list(...)
                gap_stat <- cluster::clusGap(x, FUNcluster, K.max = k.max, 
                        B = nboot, verbose = verbose, ...)
                if (!is.null(extra_args$maxSE)) 
                        maxSE <- extra_args$maxSE
                else maxSE <- list(method = "firstSEmax", SE.factor = 1)
                p <- fviz_gap_stat(gap_stat, linecolor = linecolor, 
                        maxSE = maxSE)
                return(p)
        }
}

自GNU awk 4.1.0起,您可以添加开关$ awk '$2!~/name:\s*\"."/' RS= FS="\n" file forward-zone: name: "mydomain.com" forward-addr: 10.1.1.1 来就地更改文件。
这样就可以了:

-i inplace