Just sed and nl and sort :
nl <filename> -s ":"|sort -t ":" -k 3 -k 2 | sed -n ":x s/\n[0-9 ]*$//;/end[^\n]*$/{N;bx};s/\(.*\)[ 0-9]*:end .*id:\( [0-9]*\).*\n.*start.*id:\2[^0-9]*$/\1/;tx;s/\n$//;/start/{P;D};/^[ 0-9]*:end[^\n]*/{s/\n[0-9:]*$/$/;N;bx};/start/P;/end/P;" | sort -n| sed "s/[ 0-9]*://"
nl tt -s ":"|sort -t ":" -k 3 -k 2 | sed -n ":x s/\n[0-9 ]$//;/end[^\n]$/{N;bx};s/(.)[ 0-9]:end .id:( [0-9]).\n.start.id:\2[^0-9]$/\1/;tx;s/\n$//;/start/{P;D};/^[ 0-9]:end[^\n]/{s/\n[0-9:]$/$/;N;bx};/start/P;/end/P;" | sort -n| sed "s/[ 0-9]://"
start ======== id: 5713
start ======== id: 5713
start ======== id: 5713
start ======== id: 5713
start ======== id: 5911
start ======== id: 6111
end ======== id: 31117
If the order is not important (and if every end has the start line):
sort <filename> -t ":" -k 2|sed -e '/end/{N;d;}
start ======== id: 31117
start ======== id: 5911
start ======== id: 6111
This is better (need to repair, but working):
sort <filename> -t ":" -k 2 | sed -n ":x ;/end[^\n]*$/{N;bx};s/\(.*\)end .*id:\( [0-9]*\).*start.*id:\2[^0-9]*$/\1/;tx;s/\n$//;/start/{P;D};/^end[^\n]*/{s/\n$/$/;N;bx};/start/P;/end/P"
cat tt
start ======== id: 5713
start ======== id: 5713
start ======== id: 5713
start ======== id: 5713
start ======== id: 5713
start ======== id: 5713
start ======== id: 5713
dggdgtfZZ
start ======== id: 5713
start ======== id: 5713
start ======== id: 5911
start ======== id: 5911
end ========= id: 5911
start ======== id: 6111
end ========= id: 5713
end ========= id: 5713
end ========= id: 5713
end ========= id: 5713
end ========= id: 5713
start ======== id: 31117
end ======== id: 31117
end ======== id: 31117
sort -t ":" -k 2 tt| sed -n ":x ;/end[^\n]$/{N;bx};s/(.)end .id:( [0-9]).start.id:\2[^0-9]$/\1/;tx;s/\n$//;/start/{P;D};/^end[^\n]/{s/\n$/$/;N;bx};/start/P;/end/P"
end ======== id: 31117
start ======== id: 5713
start ======== id: 5713
start ======== id: 5713
start ======== id: 5713
start ======== id: 5911
start ======== id: 6111
start ======== id: 5713
is missing – pLumo Sep 21 '21 at 15:46start
and once with anend
. The solutions in the dupe don't handle this. @Kusalananda – terdon Sep 21 '21 at 15:53start
lines without a matching (and equal number of)end
line(s) but is it also possible for the input to have anend
line without a matchingstart
line? – cas Sep 21 '21 at 17:05