I'm searching for a one-line command to get my desired output. Normal text looks like: "test_list_20160915_bla.log" Desired output: "2016/09/15"
I could do this with two awk commands (I know the command only print the year, it's just for the purpose):
echo "test_list_20160915_bla.log" |awk -F_ '$3 ~ /[0-9]/ {print $3}' |awk 'BEGIN {OFS="/"} {print substr($1,1,4)}'
But how do I use this within 1 command? Is awk even the right tool for that? Maybe sed could do the same, but I'm more familiar with awk.
I have some trouble with the provided solution. Sometimes we have files like: "test_20161205145213.log". With the sed command my output will look like "2051/45/21", which is pretty bad. Tried several things, but I can't figure it out.
Switched this
sed -r 's!^.*_([0-9]{4})([0-9]{2})([0-9]{2})_.*$!\1/\2/\3!'
to
sed -r 's!^.*(20[0-9]{2})([0-9]{2})([0-9]{2}).*$!\1/\2/\3!'
This limits the error outputs to timestamps with hour 2. I would prefer to completely ignore the filename and only go for the timestamp in it.
Timestamp could be yyymmdd_hhmmss or yyymmddhhmmss. I just need the yyyy/mm/dd part. Problem is that there is no fixed length or field seperator.