No hagas esto en un bucle de shell, awk
lo hará por ti.
Este es un script awk
que puede almacenar en un archivo de script e invocar conawk -f script.awk filename
(modificado para trabajar con la edición reciente de la pregunta):
BEGIN {
follows["foo"] = "bar"
follows["bar"] = "foo"
}
{
for (i = 1; i <= NF; ++i)
if (lookfor == "") {
if ($i in follows)
lookfor = follows[$i]
} else if ($i == lookfor) {
record[$i] = (record[$i] == "" ? "" : record[$i] OFS ) FNR
lookfor = follows[$i]
}
}
END {
for (i in record)
printf "%s:\t%s\n", i, record[i]
}
Ejecutando esto en sus datos de ejemplo:
$ awk -f script.awk file
foo: 1 12 15
bar: 5 15 21
El script configura una follows
lista asociativa. La lista dice que cuando se encuentra foo
, debemos buscar bar
, y cuando se encuentra bar
, nos interesa encontrar foo
nuevamente.
El script awk
busca las cadenas en lookfor
. Inicialmente, busca cualquiera de las claves en la matriz follows
y luego usa esta matriz para establecer lookfor
en la cadena que buscará a continuación. Cada vez que encuentra una cadena que está buscando, almacena el número de línea actual en la matriz asociativa record
para la cadena correspondiente.
Al final, se emiten los números de línea recopilados en record
.
La línea de aspecto funky -
record[$i] = (record[$i] == "" ? "" : record[$i] OFS ) FNR
agrega el número de línea al final de la cadena de números de línea en la matriz record
. Si la cadena está vacía, simplemente la establece en el número de línea actual, pero si ya contiene algo, se inserta una coma entre la cadena existente y el número de línea.
Протокол TCP Keep -был определен в то время, когда даже концепция брандмауэра, не говоря уже о брандмауэре с отслеживанием состояния или NAT, вероятно, не была широко распространена. Из RFC 1122(октябрь 1989 г.):
4.2.3.6 TCP Keep-Alives
Implementors MAY include "keep-alives" in their TCP
implementations, although this practice is not universally
accepted. If keep-alives are included, the application MUST
be able to turn them on or off for each TCP connection, and
they MUST default to off.Keep-alive packets MUST only be sent when no data or
acknowledgement packets have been received for the
connection within an interval. This interval MUST be
configurable and MUST default to no less than two hours.
[...]
Основная идея в то время заключалась не в потере информации о состоянии:
DISCUSSION:
A "keep-alive" mechanism periodically probes the other
end of a connection when the connection is otherwise
idle, even when there is no data to be sent. The TCP
specification does not include a keep-alive mechanism
because it could: (1) cause perfectly good connections
to break during transient Internet failures; (2)
consume unnecessary bandwidth ("if no one is using the
connection, who cares if it is still good?"); and (3)
cost money for an Internet path that charges for
packets.
[...]
A TCP keep-alive mechanism should only be invoked in
server applications that might otherwise hang
indefinitely and consume resources unnecessarily if a
client crashes or aborts a connection during a network
failure.
Я просмотрел обновленные RFC, но не смог точно упомянуть о сохранении активности.