N. Alaze N. Alaze - 15 days ago 7
Bash Question

How to cut the date out of a string in Shell?

I got a list of Strings like this in a .txt file

asdafdgdhjhgk.de/dsafdfdfgfdggfgg - Abgelaufen seit 26.11.2076 14:08 (seit 12345 Tagen)


Now I want to cut the date out of the strings like:
26.11.2076


All this have to happen in a Shell-Script so I through
cut
or
sed
would be a good idea but i didn't found an answer in the internet.

Answer

You can use GNU grep with -E with extended regEx support using the -E, --extended-regexp flag.

$ grep -Eo "[[:digit:]]{2}.[[:digit:]]{2}.[[:digit:]]{4}" <<< "asdafdgdhjhgk.de/dsafdfdfgfdggfgg - Abgelaufen seit 26.11.2076 14:08 (seit 12345 Tagen)"
26.11.2076

(or) if you want to run it on a file with multiple such strings, do

$ grep -Eo "[[:digit:]]{2}.[[:digit:]]{2}.[[:digit:]]{4}" input-file
Comments