这是的示例
[A]
XXXX
SSSS
[B]
EEEEE
EEEEE
[A]
ZZZZ
RRRR
[B]
TTTTT
UUUUU
[C]
OOOOO
ZZZZZ
问题是,我们如何在linux shell groupby元素中获得这样的结果?:
[A]
XXXX
SSSS
ZZZZ
RRRR
[B]
EEEEE
EEEEE
TTTTT
UUUUU
[C]
OOOOO
ZZZZZ
因此,如果能有一个函数、脚本或一组命令来完成这项任务,这将对我的自动代理和负载均衡器项目有很大帮助。谢谢
使用awk:
awk '
/^[/ {
group = $0
next
}
{
elems[group] = elems[group] $0 ORS
}
END {
for (group in elems)
printf "%s%s%s", group, ORS, elems[group]
}' file