我有一个包含以下字符串的示例文件。我需要通过删除新行(\ n)将'if'和'endif'之间的所有字符串放在一行中(\ n) 示例如下所示。我需要使用sed或bash脚本的输出。 任何人都可以帮助我吗?Shell脚本模式识别
--sample字符串
if
ABC
BCD
DEF
endif
if
123
456
789
endif
--needed ouptput
ifABCBCDDEFendif
if123456789endif
我有一个包含以下字符串的示例文件。我需要通过删除新行(\ n)将'if'和'endif'之间的所有字符串放在一行中(\ n) 示例如下所示。我需要使用sed或bash脚本的输出。 任何人都可以帮助我吗?Shell脚本模式识别
--sample字符串
if
ABC
BCD
DEF
endif
if
123
456
789
endif
--needed ouptput
ifABCBCDDEFendif
if123456789endif
假设该文件包含只有 IF-ENDIF块:
awk '{ printf("%s", $0) } /^endif$/ { printf("\n") }'
中间间隔文本应保持不变,稍微更复杂的方法称为f或:
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'
这里是输出ifABCBCDDEFendif if123456789endif –
你使用哪个版本的sed工作? –
如果你的文件file.txt的那么
cat file.txt | tr -d "\n" | sed '[email protected]@endif\[email protected]'
你好迈克尔,这个b将所有if块同时放入一行,但我需要单独使用Ifendif块,如上所示。 – HungryProgrammer
@HungryProgrammer对不起,我不关注。 awk脚本将每个ifendif块放在它自己的行上。 –