搜索以“zone”开头的行组,并在上一行的开头添加“Heading”字样。感谢您的帮助
代码:
Zoning A
zone zonename1
zone zonename2
zone zonename3
Team B
zone zonename4
zone zonename5
期望的输出:
Heading Zoning A
zone zonename1
zone zonename2
zone zonename3
Heading Team B
zone zonename4
zone zonename5
答案 0 :(得分:2)
使用awk。只要组标题不以单词 zone 开头:
$ awk '
/^ *zone/&&p!~/^ *zone/ {
sub(/^ */,"&Heading ",p)
}
NR>1 { print p }
{ p=$0 }
END { print p }
' file
Heading Zoning A
zone zonename1
zone zonename2
zone zonename3
Heading Team B
zone zonename4
zone zonename5
答案 1 :(得分:1)
如果您可以使用perl
:
perl -000 -pe 's/^/Heading/;s/\s+/ /' inputfile
Heading Zoning A
zone zonename1
zone zonename2
zone zonename3
Heading Team B
zone zonename4
zone zonename5