首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将文本块移动到范围的末尾(使用sed)?

如何将文本块移动到范围的末尾(使用sed)?
EN

Stack Overflow用户
提问于 2019-09-13 20:19:36
回答 2查看 39关注 0票数 1

因为我搜索了这么多次,都找不到一个简单的(故障安全)解决方案。

问题

考虑下面的代码片段。其中,我想将块BLOCK_ONE移动到范围RANGE_END的末尾。

代码语言:javascript
复制
if(condition)
    string(RANGE_BEGIN          # RANGE_BEGIN
        "$<$<BLOCK_ONE>:"       # BLOCK_ONE BEGIN
            "Boost::system;"
            "Boost::filesystem;"
        ">"                     # BLOCK_ONE END
        "$<$<BLOCK_TWO>:"
            "otherlib;"
            "somelib;"
        ">" 
        "$<$<BLOCK_THREE>:"
            "comctl32;"
        ">"
    )                           # RANGE_END
elseif(othercondition)
    string(RANGE_BEGIN          # RANGE_BEGIN
        "$<$<BLOCK_ONE>:"       # BLOCK_ONE BEGIN
            "pthread;"
            "Zlib::minizip;"
        ">"                     # BLOCK_ONE END
        "$<$<BLOCK_FOUR>:"
            "somelib;"
        ">"
    )                           # RANGE_END
endif()

目标

代码语言:javascript
复制
if(condition)
    string(RANGE_BEGIN          # RANGE_BEGIN
        "$<$<BLOCK_TWO>:"
            "otherlib;"
            "somelib;"
        ">" 
        "$<$<BLOCK_THREE>:"
            "comctl32;"
        ">"
        "$<$<BLOCK_ONE>:"       # BLOCK_ONE BEGIN
            "Boost::system;"
            "Boost::filesystem;"
        ">"                     # BLOCK_ONE END
    )                           # RANGE_END
elseif(othercondition)
    string(RANGE_BEGIN          # RANGE_BEGIN
        "$<$<BLOCK_FOUR>:"
            "somelib;"
        ">"
        "$<$<BLOCK_ONE>:"       # BLOCK_ONE BEGIN
            "pthread;"
            "Zlib::minizip;"
        ">"                     # BLOCK_ONE END
    )                           # RANGE_END
endif()
EN

回答 2

Stack Overflow用户

发布于 2019-09-13 20:30:23

警告:如果BLOCK_ONE为而不是 found,这将无法正常工作。(它会将括号移动到范围内的下一个括号。)

下面是我使用的解决方案:

代码语言:javascript
复制
sed '/RANGE_BEGIN/,/)/{     # Do the following within given range (/BEGIN/,/END/)
        /BLOCK_ONE/,/">"/{  # Do the following within given range (/BEGIN/,/END/)
            /BLOCK_ONE/h    # BLOCK_ONE containing line: overwrite hold space with it
            /BLOCK_ONE/!H   # BLOCK_ONE non-containing line: append it to hold space
            d               # Delete what you read (past tense)
        }
        /)/{                # End of RANGE
            x               # eXchange pattern with hold space 
                            # (replace the line with everything you collected before, 
                            # keeping the replaced line in hold space)
            G               # Add newline and Get the line we saved with x printed
        }
}' InputFile.cmake

对于喜欢一句话的人,这里就是:

代码语言:javascript
复制
sed '/RANGE_BEGIN/,/)/{/BLOCK_ONE/,/">"/{/BLOCK_ONE/h;/BLOCK_ONE/!H;d};/)/{x;G}}' InFile
票数 1
EN

Stack Overflow用户

发布于 2019-09-13 22:22:06

这可能适用于您(GNU sed):

代码语言:javascript
复制
sed -n '/BLOCK_ONE/{h;:a;n;H;/">"/!ba;:b;n;/)/!{p;bb};H;x};p' file

聚焦于包含BLOCK_ONE的一行,然后在等待空间中收集更多行,直到包含">"的行。继续打印一行,直到一行包含),然后将该行附加到保留空间,交换到保留空间,并打印这些行。所有其他行都会照常打印。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57923516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档