Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use groupby or aggreation in linux

here is the exemple

[A]
XXXX
SSSS
[B]
EEEEE
EEEEE
[A]
ZZZZ
RRRR
[B]
TTTTT
UUUUU
[C]
OOOOO
ZZZZZ

the question is how can we do in linux shell groupby element to have this result ? :

[A]
XXXX
SSSS
ZZZZ
RRRR
[B]
EEEEE
EEEEE
TTTTT
UUUUU
[C]
OOOOO
ZZZZZ

So if can have a function or script or set of commands to do this task that would help me a lot in my project of auto proxy and load balancers. Thank you

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Using 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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading