How to get previous month and previous year in MonthYear format (December2020) using Linux - linux

I want the previous month & previous year in the format: "December2020".
But while executing the below Linux command I'm getting July2020.
echo `date -d "2021-01-08" '+%B' -d 'last month'``date -d '1 year ago' +%Y
The date hardcoded parameter is in format: YYYY-MM-DD (2021-01-08)

date accepts only one -d option. In your command date -d "2021-01-08" '+%B' -d 'last month' the first -d is ignored. Only the -d "last month" applies. Because of that, and since we have August right now, the output is July.
You probably wanted to use
date -d '2021-01-08 - 1 month' +%B%Y
which prints December2020.
If you really wanted to concat the previous month and previous year together, you could use
echo "$(date -d'2021-01-08 - 1 month' +%B)$(date -d'2021-01-08 - 1 year' +%Y)"
but that would give rather strange results:
2021-01-08 → December2020 # 1 month before input date
2021-04-30 → March2020 # 13 months before input date

Related

Extract 1 month older data using Shell scripts [duplicate]

This question already has answers here:
Filter log file entries based on date range
(5 answers)
Closed 5 years ago.
I need to find the entries from a huge log file which are 1 month older than the current date using shell scripts. The log entry date is in the 3rd field of each line.
This is my sample log file:
0114374510,OK,10/23/2017 9:22:50 AM,0016692200,OK
0112364510,OK,10/23/2017 9:22:50 AM,0016692200,OK
0112364510,TX,10/21/2017 9:10:00 AM,0016692200,OK
0115364510,TX,10/21/2017 10:52:00 AM,0016692200,OK
0112368979,OK,7/29/2016 10:25:00 AM,0000718374,OK
0113368979,OK,7/29/2016 12:50:00 PM,0000718374,OK
0112368979,TX,9/16/2015 10:57:00 AM,0033545820,OK
Thanks!
Here is a solution, using grep to match certain lines in your file and date to manipulate dates.
Linux version
grep -E "$(date --date='-1 month' '+%m')/[0-9]+/$(date --date='-1 month' +'%Y') <your_file>"
date --date='-1 month' '+%m' - Subtract 1 month to the current date and print the month
[0-9]+ - Regex matching any number (any day in our case)
$(date --date='-1 month' +'%Y') - Subtract 1 month to the current date and print the year
macOS version
grep -E "$(date -v -1m '+%m')/[0-9]+/$(date -v -1m +'%Y') <your_file>"
date -v -1m '+%m' - Subtract 1 month to the current date and print the month
[0-9]+ - Regex matching any number (any day in our case)
date -v -1m +'%Y' - Subtract 1 month to the current date and print the year

Convert specific date format to Epoch in linux

I need to convert this date format to epoch : 03/Apr/2016 14:22:59
the command
date -d "03/Apr/2016 14:22:59" +"%s"
will return :
date: invalid date ‘03/Apr/2016 14:22:59
Anyone can help me format it in a way it become recognizable by date -d ?
Thanks in advance.
Perl to the rescue:
perl -MTime::Piece -e 'print Time::Piece
->strptime("03/Apr/2016 14:22:59", "%d/%b/%Y %H:%M:%S")
->epoch'
Info page for date input formats can be shown with following command:
info date "Date input formats"
Unfortunately your date format is not supported by date. You can however convert your string into format that is supported for example like this:
date -d "$(echo '03/Apr/2016 14:22:59' | tr -s "/" "-")" +"%s"
To provide information about which kind of input strings can be used for date command I will write here a short summary:
Show date from epoch
date -d "#1513936964"
Words like today, tomorrow, month names (January), AM/PM, time zone names are supported
date -d "tomorrow 10:00:30PM"
date -d "03 April 2016 14:22:59"
Calendar formats
date -d "04/03/2016 14:22:59"
date -d "03-Apr-2016 14:22:59"
date -d "2016-04-03 14:22:59.00"
Timezones
date -d "PDT now"
Which day was Christmas last year?
date -d "$(date -d "2017-12-24") -1 year" +"%A"
Using Python:
python -c 'from time import strftime, strptime;print strftime("%s", strptime("03/Apr/2016 14:22:59", "%d/%b/%Y %H:%M:%S"))'

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

change the call from a bash script in a month to a week

I have 2 script in bash, and i have some files:
transaction-2012-01-01.csv.bz2
transaction-2012-01-02.csv.bz2
transaction-2012-01-03.csv.bz2
transaction-2012-01-04.csv.bz2
.
.
transaction-2012-01-31.csv.bz2
transaction-2012-02-01.csv.bz2
.
.
transaction-2012-02-28.csv.bz2
I have a script called script.sh
cat script.sh
YEAR_MONTH=$1
FILEPATH="transaction-$YEAR_MONTH*.csv.bz2"
bzcat $FILEPATH|strings|grep -v "code" >> output
And if you need call the script you can use other script
cat script2.sh
LAST_MONTH=$(date -d -1month +%Y"-"%m)
if [ $# -eq 1 ]; then
DATE=$1
else
DATE=$LAST_MONTH
fi
script.sh $DATE 1>output$DATE.csv 2>> log.txt
And it do cat the files in a month, but now i need call the script with a specific week in a year:
bash script2.sh 2012-01
where 2012 is the year and 01 is the month
Now i need call the script with:
bash script2.sh 2012 13
where 2012 is the year and 13 is the week in a year
Now i need the cat only to the files in the year and week that the user specified, no per month per week
But the format of the files do not help me!!!! because the name is transaction-year-month-day.csv.bz2, not transaction-year-week.csv.bz2
Take a look at the manpage for strftime. These are date format codes. For example:
$ date +"%A, %B %e, %Y at %I:%m:%S %p"
Will print out a date like:
Thursday, May 30, 2013 at 02:05:31 PM
Try to see why this works.
On some systems, the date command will have a -j switch. This means, don't set the date, but reformat the given date. This allows you to convert one date to another:
$ date -f"$input_format" "$string_date" +"$output_format"
The $input_format is the format of your input date. $string_date is the string representation of the date in your $input_format. And, $output_format is the format you want your date in.
The first two fields are easy. Your date is in YY-MM-DD format:
$ date -f"%Y-%m-%d" "$my_date_string"
The question is what can you do for the final format. Fortunately, there is a format for the week in the year. %V which represents the weeks at 01-53 and %W which represents the weeks as 00-53.
What you need to do is find the date string on your file name, then convert that to the year and week number. If that's the same as the input, you need to concatenate this file.
find $dir -type f | while read transaction_file
do
file_date=${transaction_file#transaction-} #Removes the prefix
file_date=${file_date%.csv.bz2} #Removes the suffix
weekdate=$(date -j -f"%Y-%m-%d" "$file_date" +"%Y %W")
[ "$weekdate" -eq "$desired_date" ] || continue
...
done
For example, someone puts in 2013 05 as the desired date, you will go through all of your files and find ones with dates in the range you want. NOTE: That the week of the year is zero filled. You may need to zero fill the input of the week number to match.

Convert string to date in bash

I have a string in the format "yyyymmdd". It is a string in bash and I want to get it converted into a date so that all other date functions can be used on it.
"20121212" string into "20121212" date with format "%Y%m%d".
This worked for me :
date -d '20121212 7 days'
date -d '12-DEC-2012 7 days'
date -d '2012-12-12 7 days'
date -d '2012-12-12 4:10:10PM 7 days'
date -d '2012-12-12 16:10:55 7 days'
then you can format output adding parameter '+%Y%m%d'
We can use date -d option
1) Change format to "%Y-%m-%d" format i.e 20121212 to 2012-12-12
date -d '20121212' +'%Y-%m-%d'
2)Get next or last day from a given date=20121212. Like get a date 7 days in past with specific format
date -d '20121212 -7 days' +'%Y-%m-%d'
3) If we are getting date in some variable say dat
dat2=$(date -d "$dat -1 days" +'%Y%m%d')
date only work with GNU date (usually comes with Linux)
for OS X, two choices:
change command (verified)
#!/bin/sh
#DATE=20090801204150
#date -jf "%Y%m%d%H%M%S" $DATE "+date \"%A,%_d %B %Y %H:%M:%S\""
date "Saturday, 1 August 2009 20:41:50"
http://www.unix.com/shell-programming-and-scripting/116310-date-conversion.html
Download the GNU Utilities from Coreutils - GNU core utilities (not verified yet)
http://www.unix.com/emergency-unix-and-linux-support/199565-convert-string-date-add-1-a.html
just use the -d option of the date command, e.g.
date -d '20121212' +'%Y %m'

Resources