如何在sed模式的第二次匹配之前删除所有行?

时间:2018-03-30 14:10:43

标签: bash unix sed

我有以下文件:

public <T> PageImpl<T> runQuery(final Statement statement, final Expression expression, final String alias,
        final Pageable pageable, final Class<T> tClazz) {
    PageImpl<T> tPage = null;
    try {
        CompletableFuture<List<T>> entityFuture = CompletableFuture
                .supplyAsync(() -> findByN1QLProjection(statement, tClazz)).exceptionally(th -> {
                    logger.write(new Exception(th.getMessage(), th));
                    return Collections.emptyList();
                });
        CompletableFuture<Long> countFuture = CompletableFuture.supplyAsync(() -> doCount(expression, alias))
                .exceptionally(th -> {
                    logger.write(new Exception(th.getMessage(), th));
                    return 0L;
                });
        CompletableFuture.allOf(entityFuture, countFuture).get();
        tPage = new PageImpl<>(entityFuture.get(), pageable, countFuture.get());
    } catch (final InterruptedException | ExecutionException ex) {
        logger.write(ex);
        tPage = new PageImpl<>(Collections.emptyList(), pageable, 0);
    }
    return tPage;
}

使用first second third fourth third fifth sixth 我可以从第一场比赛开始打印,以获得:

cat file | sed -n '/third/,$p'

是否可以修改third fourth third fifth sixth 命令,使其基本上忽略第一个匹配并从第二个匹配打印?那将是:

sed

3 个答案:

答案 0 :(得分:2)

这是一个awk,它通过保持缓冲区来存储行中third出现的所有行,并在再次找到third时重置缓冲区来执行此操作:

awk '/third/{p=$0 RS; next} p{p=p $0 RS} END{printf "%s", p}' file

third
fifth
sixth

或者,您可以使用此tac + awk

tac file | awk '1; /third/{exit}' | tac

third
fifth
sixth

答案 1 :(得分:2)

使用sed:

sed '1,/third/d' file | sed -n '/third/,$p'

输出:

third
fifth
sixth

答案 2 :(得分:0)

使用gnu sed

sed '/third/!d;:A;N;/\nthird/!{s/[^\n]*\n//;bA };s/[^\n]*\n//;:B;N;bB' infile
相关问题