Vim formatted print date to current file - bash

I am trying to print the current formatted as Mon, 1 Jan 2022 into my current file in vim. So far I've tried the following command, but the % sign gets put into the command as the current file.
:0r !date '+%a, %d %b %Y'
This ends up giving me an output similar to seen below.
/home/user/file.txta, /home/user/file.txtd /home/user/fileb /home/user/fileY
The issue is that the path to the current file is put in place for % as vim does, but that is causing issues for the current use case.
Any help is appreciated

The immediate solution is obviously to escape the %:
:0r !date '+\%a, \%d \%b \%Y'
But you might be interested by the built-in :help strftime(), which doesn't require escaping:
:0put=strftime('%a, %d %b %Y')

Related

Bash shell: How to reformat string date from a variable value

I understand how to reformat a date using the date command, and I am fine with that. However, I have a wrinkle in that I am struggling with - the date I want to reformat is the output of another command, so I am storing it in the variable. I am struggling with the syntax of how to specify that I want to take the output of one command, and run it through date -d, and store it in another variable. Here is what I tried:
expdate=`get_expire_date.sh`
echo $expdate
Mon 23 Mar 2022 05:05:05 PM UTC
expdval=`date -d'($expdate)'`
echo $expdval
I get today's date, not the converted expire date from the script output. If I leave the parenthesis out, of course, it treats $expdate as the literal text to translate and gives an error, whereas if I leave the single quote marks off, it uses the spaces in the date string as a delimiter and only grabs the first token.
What am I doing wrong?
First, parameter expansion doesn't occur inside single quotes. You would need to change the single quotes
expdval=`date -d'($expdate)'`
to double quotes
expdval=`date -d"($expdate)"`
Second, the parentheses create an invalid input, which results (for reasons I don't really understand) in an output of midnight of the current day. (You'll get the same result with the trivial invalid date date -d "".)
Drop the parentheses, and you'll get the same date back (because the input format matches the default output format).
$ date -d "$expdate"
Wed Mar 23 13:05:05 EDT 2022
To actually manipulate it, you'll need an explicit output format:
$ date -d "$expdate" +%Y-%m-%d
2022-03-23
or some form of date "arithmetic":
$ date -d "$expdate + 2 days"
Fri Mar 25 13:05:05 EDT 2022
I found I had to use double-quotes instead, like this (and sorry for the old way of doing things, updating to new shell syntax):
expdval=$(date -d"$(get_expire_date.sh)")

Simplify complex command, put it into a variable

date +'%A %B %d' | sed -e 's/\(^\|[^[:digit:]]\+\)0\+\([[:digit:]]\)/\1\2/g
I like the output of the above command, which strips leading zeroes off days of the month produced by the date command, in the case of numerals less than 10. It's the only way I've thus far found of producing single digit dates from the date command's output for the day of the month, which otherwise would be 01, 02, 03, etc.
A couple of questions in this regard. Is there a more elegant way of accomplishing the stated goal of stripping off zeroes? I do know about date's %e switch and would like to use it, but with numerals 10 and greater it has the undesirable effect of losing the space between the month name and the date (so, July 2 but July10).
The second question regards the larger intended goal of arriving at such an incantation. I'm putting together a script that will scrape some data from a web page. The best way of locating the target data on the page is by searching on the current date. But the site uses only single digits for the first 9 days of the month, thus the need to strip off leading zeroes. So what's the best way of getting this complex command into a variable so I can call it within my script? Would a variable within a variable be called for here?
RESOLUTION
I'll sort of answer my own question here, though it is really input from Renaud Pacalett (below) that enabled me to resolve the matter. His input revealed to me that I'd not understood very well the man page, particularly the part where is says "date pads numeric fields with zeroes," and below that where it is written "- (hyphen) do not pad the field." Had I understood better those statements, I would have realized that there is no need for the complex sed line through which I piped the date output in the title of this posting: had I used there %-d instead of just %d there would have been no leading zeroes in front of numerals less than 10 and so no need to call sed (or tr, as suggested below by LMC) to strip them off. In light of that, the answer to the second question about putting that incantation into a variable becomes elementary: var=$(date +'%A %B %-d') is all that is needed.
I may go ahead and mark Renaud Pacalet's response as the solution since, even though I did not implement all of his suggestions into the latest incarnation of my script, it proved crucial in clarifying key requirements of the task.
If your date utility supports it (the one from GNU coreutils does) you can use:
date +'%A %B %-d'
The - tells date to not pad the numeric field. Demo:
$ date -d"2021/07/01" +'%A %B %-d'
Thursday July 1
Not sure I understand your second question but if you want to pass this command to a shell script (I do not really understand why you would do that), you can use the eval shell command:
$ cat foo.sh
#!/usr/bin/env bash
foo="$(eval "$1")"
echo "$foo"
$ ./foo.sh 'date -d"2021/07/01" +"%A %B %-d"'
Thursday July 1
Please pay attention to the double (") and simple (') quotes usage. And of course, you will have to add to this example script what is needed to handle errors, avoid misuses...
Note that many string comparison utilities support one form or another of extended regular expressions. So getting rid of these leading zeros or spaces can be as easy as:
grep -E 'Thursday\s+July\s+0*1' foo.txt
This would match any line of foo.txt containing
Thursday<1 or more spaces>July<1 or more spaces><0 or more zeros>1

Concatenating text and command output in Bash script to output to file

I'm writing a bash script in FreeBSD that appends commands to a log file. Before I execute the commands that will append the log data, I want to print a line in the file which shows the current date above the data, like this:
---Tue Aug 20 17:26:37 EDT 2019---
I know that I can use the date command to output the timestamp, but I'm not sure how to include the "---" before and after the timestamp to add to the file. What's the simplest way to do this?
You can pass a format string to date:
date '+---%a %b %e %H:%M:%S %Z %Y---'

`date` formatting to specifically add dots between the AM and PM markers

I use a command quite frequently called datey that I run by invoking ./datey which will generate this date: "09/15/14 — 03:42:37 AM". Nothing special, a very simple one line command.
I have always wanted the output to be slightly different, but have been unable to make that happen. The command I am calling underneath all this is: date. In it's simplest form, it is:
#!/bin/bash
date "+%D — %r"
Which results in:
09/15/14 — 03:45:45 AM
What I want:
09/15/14 — 03:45:55 A.M.
Or:
09/15/14 — 03:46:23 P.M.
Basically, just adding in dots in the A.M. and P.M. portion of the output. I am not sure if there is a standard, but to me, in the US, using dots is sort of a convention, that, at least in my geographic region, using dots.
Suggestions?
You could remove the trailing M and add .M. to the end of the string:
$ d=$(date "+%D — %r")
$ echo "${d%M}.M."
09/15/14 — 07:25:14 A.M.
One way is to use sed on date's output:
date "+%D — %r" | sed 's/\([AP]\)\(M\)/\1.\2./'
09/15/14 — 06:53:42 A.M.
You can use sed to make a little transform
# date "+%D - %r"|sed s/M/.M./g
09/15/14 - 12:53:10 P.M.

How can I append time to this command prompt date command

I am trying to run a .bat file with command prompt to add time to the date.
Currently, I have this code
MOVE...\folder\^"Mytest %DATE:/=-%.csv^"
This produces
..\folder\Mytest Thu 12-06-2012.csv
I want to get
..\folder\Mytest Thu 12-06-2012 21:45.csv
Tried all kinds of things but failed miserably. Help would be greatly appreciated.
This will work:
%date:/=-% %time:~0,5%.csv
The %time% uses the current time; the :~ means "a substring of", and the 0,5 says "starting at the first character (index 0) and continuing for 5 characters", so the entire thing means "give me the first 5 characters of the output of time".
Using this at a command prompt:
C:\>echo %date:/=-% %time:~0,5%
outputs
Thu 12-06-2012 18:19
The format you're using is going to cause problems with sorting, though. My advice would be to drop the day of the week portion, and change the date output to CCYY-MM-DD, which will be much more useful when you're trying to find a specific date. You can use this:
echo %date:~10,4%-%date:~4,2%-%date:~7,2% %time:~0,5%
which outputs
2012-12-06 18:33

Resources