简体   繁体   中英

Extract month and day from linux “date” command

I would like to modify the following statement to extract the Month, as well as day:

testdate=$(date -d "2 days" +%d| sed 's/0([1-9])/ \\1/')

right now it only extracts the day...

I'm currently googling how sed works. looks like a regular expression of sorts that's being used but... I thought I'd check with the forum for certain.

Thanks.

EDIT 1

now I have:

  testdate=$(date -d "2 days" "+%b %_d %Y")

Which is great... except when the leading zeros in the day are stripped out. then i end up with two spaces between the Month and day for example, for April 29, 2014, I get:

  Apr 29 2014

which is great. But for May 01, 2014 I get:

  May  1, 2014

where there's a double space between "May" and "1". I guess i can do it in two steps... by doing the date command, followed by sed to find and replace double spaces. Just wondering if there's a way to do it all in one command. Thanks.

date accepts a FORMAT arg which should be used here. sed is not necessary:

date +%d.%m.

Outputs:

03.04.

in European time format. If you want the month's name printed use

date +'%d. %B'

Output:

03. April

To read the month and day into shell variables (assuming bash/ksh/zsh)

read month day < <(date -d "2 days" "+%m %d")

If you're planning to do arithmetic on these values, be of numbers that would be treated as octal but are invalid octal numbers 08 and 09 . If you want to strip off the leading zero, use "+%_m %_d"


Using read , the shell takes care of the excess whitespace for you:

read mon day year < <(date -d "2 days" "+%b %_d %Y")
testdate="$mon $day $year"

If you don't want to use the temp vars:

testdate="Apr  5 2014"       # $(date -d "2 days" "+%b %_d %Y")
testdate=${testdate//  / }   # globally replace 2 spaces with 1 space
echo "$testdate"

For date format %d would print only the day. It's unlikely to extract month unless you specify that in the format.

Specify the format for obtaining the month too.

   %b     locale's abbreviated month name (e.g., Jan)
   %B     locale's full month name (e.g., January)
   %m     month (01..12)

For example:

$ date -d "2 days" +%d/%b
05/Apr
$ date -d "2 days" +%d/%B
05/April
$ date -d "2 days" +%d/%m
05/04

Seems there is a simpler way to extract [the] month and day from [the] linux “date” command, then all the previous answers.

From $ date --help :

  By default, date pads numeric fields with zeroes.
  The following optional flags may follow `%':

  -  (hyphen) do not pad the field

Which gives us the below, without the use of sed or read to remove any kind of padding:

testdate=$(date -d "3 days" "+%b %-d %Y") ( days adjusted to show single-digit day number, from today's date)

And outputs the below:

$ echo $testdate
Sep 1 2014

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM