0

我有一个包含以下字符串的示例文件。我需要通过删除新行 (\n) 将 'if' 和 'endif' 之间的所有字符串放在一行中。示例如下所示。我需要使用 sed 或在 bash 脚本中的输出。谁能帮我??

--示例字符串

if
ABC
BCD
DEF
endif
if 
123
456
789
endif

--需要的输出

ifABCBCDDEFendif

if123456789endif
4

3 回答 3

1

假设文件包含if-endif 块:

awk '{ printf("%s", $0) } /^endif$/ { printf("\n") }'

对于不应更改的中间文本,需要一种稍微复杂的方法:

awk '/^if$/    { InIf = 1 } 
     /^endif$/ { InIf = 0 } 
               { printf("%s", $0) } 
     !InIf     { printf("\n") }'
于 2013-01-16T11:59:37.200 回答
0

假设您有包含上述字符串的文件 tt1 然后使用

cat tt1 | sed  's/\n//g' | sed  's/endif/endif\n/g'
于 2013-01-16T12:10:36.977 回答
0

一个班轮将做到这一点

如果你的文件是 file.txt 那么

命令

cat file.txt | tr -d "\n" | sed 's@endif@endif\n@g'
于 2014-03-20T11:04:59.820 回答