Issue
I need to identify the head of a list of configuration entries in a file. There is not predictable, it could be any string, but it always be the line that starts closer to the left than the others (excluding "exit"):
Here is an example:
vpls 2662 customer 1 v-vpls vlan 2662 create
description "RES_2662"
mac-move
allow-res-res
allow-reg-res
exit
stp
shutdown
exit
ingress
qos 2
exit
sap lt:1/1/1:2662 create
description "RES_2662"
enable-stats
no shutdown
exit
sap lag-1:2662 create
no shutdown
exit
no shutdown
exit
vpls 2663 customer 1 v-vpls vlan 2663 create
description "RES_2663"
mac-move
allow-res-res
allow-reg-res
exit
stp
shutdown
exit
ingress
qos 2
exit
sap lt:1/1/1:2663 create
description "RES_2663"
enable-stats
no shutdown
exit
sap lag-1:2663 create
no shutdown
In this case, I need to be able to identify the two lines that start with:
vpls 266X customer 1 v-vpls vlan 266X create
The script should know that these are the lines that I'm looking for.
The output not always will show spaces at the left, like in this example:
port vlan-port:1/1/1/3/7/4/4:824
admin-up
severity no-value
exit
port vlan-port:1/1/1/3/7/4/4:1224
admin-up
severity no-value
exit
In this case, the desired lines are:
port vlan-port:x/x/x/x/x/x/x/x
I don't know if it can be done using grep/sed/awk.
Thanks for your assistance.
Solution
The following will work using any awk in any shell on every Unix box and will retain the order of the input lines for the output in case that matters:
$ cat tst.awk
$1 != "exit" {
match($0,/^ */)
if ( (min == "") || (RLENGTH <= min) ) {
min = RLENGTH
lines[min,++cnt[min]] = $0
}
}
END {
for (i=1; i<=cnt[min]; i++) {
print lines[min,i]
}
}
$ awk -f tst.awk file
vpls 2662 customer 1 v-vpls vlan 2662 create
vpls 2663 customer 1 v-vpls vlan 2663 create
Answered By - Ed Morton Answer Checked By - Gilberto Lyons (WPSolving Admin)