我有一个包含以下字符串的示例文件。我需要通过删除新行 (\n) 将 'if' 和 'endif' 之间的所有字符串放在一行中。示例如下所示。我需要使用 sed 或在 bash 脚本中的输出。谁能帮我??
--示例字符串
if
ABC
BCD
DEF
endif
if
123
456
789
endif
--需要的输出
ifABCBCDDEFendif
if123456789endif
我有一个包含以下字符串的示例文件。我需要通过删除新行 (\n) 将 'if' 和 'endif' 之间的所有字符串放在一行中。示例如下所示。我需要使用 sed 或在 bash 脚本中的输出。谁能帮我??
--示例字符串
if
ABC
BCD
DEF
endif
if
123
456
789
endif
--需要的输出
ifABCBCDDEFendif
if123456789endif
假设文件只包含if-endif 块:
awk '{ printf("%s", $0) } /^endif$/ { printf("\n") }'
对于不应更改的中间文本,需要一种稍微复杂的方法:
awk '/^if$/ { InIf = 1 }
/^endif$/ { InIf = 0 }
{ printf("%s", $0) }
!InIf { printf("\n") }'
假设您有包含上述字符串的文件 tt1 然后使用
cat tt1 | sed 's/\n//g' | sed 's/endif/endif\n/g'
一个班轮将做到这一点
如果你的文件是 file.txt 那么
cat file.txt | tr -d "\n" | sed 's@endif@endif\n@g'