2

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.

1
  • I edited the startpost again. I only want the timestamp out of the filename. Everything else doesen't matter. But sed seems so read the string from the back. That leads sometimes to errors where he mistaken the time (hour_minute) with (year_mm_dd) Timestamp could be yyymmdd_hhmmss or yyymmddhhmmss. I just need the yyyy/mm/dd part. Problem is that there is no fixed lenght or field seperator
    – M.S.
    Commented Dec 6, 2016 at 7:48

1 Answer 1

2

You can slice and dice in the same awk script:

echo "test_list_20160915_bla.log" |
    awk -F_ '$3 ~ /^[1-9][0-9]*$/ { print substr($3,1,4) "/" substr($3,5,2) "/" substr($3,7,2) }'

Here, we just extract the year, month and day digit groups from the third "_"-separated field and print them out.

Or with sed, which also handles your additional requirement:

(
    echo "test_list_20160915_bla.log"
    echo "test_20161205145213.log"
) |
    sed -r 's!^.*_([0-9]{4})([0-9]{2})([0-9]{2}).*$!\1/\2/\3!'

Here, we use a Regular Expression to capture three digit groups for year, month and day, discarding everything else, and then print the three groups out joined by /.

4
  • Ahh damn. Now that I see it, it seems so easy. Thanks mate! The sed Command is useful, because its not limited to $3. The date could be everywhere. Thats nice!
    – M.S.
    Commented Sep 15, 2016 at 8:36
  • Well I have some trouble. 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 cant figure it out
    – M.S.
    Commented Dec 5, 2016 at 13:53
  • This topic has no working solution. If some is provided I would glady marked it as an answer
    – M.S.
    Commented Dec 6, 2016 at 11:41
  • @M.S. there you go Commented Dec 6, 2016 at 13:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .