因为我搜索了这么多次,都找不到一个简单的(故障安全)解决方案。
问题
考虑下面的代码片段。其中,我想将块BLOCK_ONE移动到范围RANGE_END的末尾。
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()目标
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()发布于 2019-09-13 20:30:23
警告:如果BLOCK_ONE为而不是 found,这将无法正常工作。(它会将括号移动到范围内的下一个括号。)
下面是我使用的解决方案:
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对于喜欢一句话的人,这里就是:
sed '/RANGE_BEGIN/,/)/{/BLOCK_ONE/,/">"/{/BLOCK_ONE/h;/BLOCK_ONE/!H;d};/)/{x;G}}' InFile发布于 2019-09-13 22:22:06
这可能适用于您(GNU sed):
sed -n '/BLOCK_ONE/{h;:a;n;H;/">"/!ba;:b;n;/)/!{p;bb};H;x};p' file聚焦于包含BLOCK_ONE的一行,然后在等待空间中收集更多行,直到包含">"的行。继续打印一行,直到一行包含),然后将该行附加到保留空间,交换到保留空间,并打印这些行。所有其他行都会照常打印。
https://stackoverflow.com/questions/57923516
复制相似问题