I'm trying to find a solution or a proper tool for the following job: I need to sort a text document with indented sections, so all levels of indentation are sorted independently for each section.
Particularly, I need this for Cisco routers' running config files to compare them with configuration templates.
Input example:
! comment
command d
command b
command c
!
interface b
podcommand b
podcommand a
!
interface a
podcommand b
podcommand a
!
command a
Expected output:
command a
command b
command c
command d
interface a
podcommand a
podcommand b
interface b
podcommand a
podcommand b
I've checked sort man page and looked for similar awk scripts, bot to no avail... Any ideas for a quick solution?
This will work on your certainly simplified sample file. Not sure it will on real data...:
awk '/!/{next} !/^ +/ {S=$0; print; next} {$0=S " " $0} 1' file | sort | awk 'NF>2 {$1=$2=""}1'
command a
command b
command c
command d
interface a
podcommand a
podcommand b
interface b
podcommand a
podcommand b
Following minor modifications to take into account real Cisco configs (e.g. multi-word section names and different white spaces), the final script looks like this: