Solución:
Usando puro bash:
$ cat file.txt
US/Central - 10:26 PM (CST)
$ while read a b time x; do [[ $b == - ]] && echo $time; done < file.txt
otra solución con bash regex:
$ [[ "US/Central - 10:26 PM (CST)" =~ -[[:space:]]*([0-9]{2}:[0-9]{2}) ]] &&
echo ${BASH_REMATCH[1]}
otra solución usando grep
y expresión regular avanzada de búsqueda:
$ echo "US/Central - 10:26 PM (CST)" | grep -oP "-s+Kd{2}:d{2}"
otra solución usando sed:
$ echo "US/Central - 10:26 PM (CST)" |
sed 's/.*- *([0-9]{2}:[0-9]{2}).*/1/'
otra solución usando perl:
$ echo "US/Central - 10:26 PM (CST)" |
perl -lne 'print $& if /-s+Kd{2}:d{2}/'
y el último usando awk:
$ echo "US/Central - 10:26 PM (CST)" |
awk '{for (i=0; i<=NF; i++){if ($i == "-"){print $(i+1);exit}}}'
echo "US/Central - 10:26 PM (CST)" | sed -n "s/^.*-s*(S*).*$/1/p"
-n suppress printing
s substitute
^.* anything at the beginning
- up until the dash
s* any space characters (any whitespace character)
( start capture group
S* any non-space characters
) end capture group
.*$ anything at the end
1 substitute 1st capture group for everything on line
p print it
Técnica chop-chop rápida y sucia, sin expresiones regulares y de baja robustez
string="US/Central - 10:26 PM (CST)"
etime="${string% [AP]M*}"
etime="${etime#* - }"
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)