Search code examples
awkcut

Getting current day number with awk and date


The output of date command has two spaces when the day number is less than 10. For example

Mon May 31 10:24:01 +0430 2020
Mon Jun  1 10:24:01 +0430 2020

For the first one, date | cut -d " " -f 3 returns 31. But, for the second one, it returns SPACE. Any way to fix that?


Solution

  • You could simple try awk to get the 3rd field, which will be definitely better than cut command.

    date | awk '{print $3}'
    

    Now why cut command is not working IMHO because space between May and 31 is equal but space between Jun and 1 is not equal so you need to use f4 in your cut command(for single digit date numbers with your current approach). Its better to use awk in this case.

    Or In ideal scenario if you want to get only date then use date '+%d' to get date always in 2 digit as per @anubhava sir's comments.