give the input format when converting a date - linux

I read the date from a file to a variable. The date has the format ddmmyyyy. It has to be converted to yyyy-mm-dd
I already searched this forum and got this far :
date -d '$DATE' +%F
The problem is the input format is not recognised. Is there any way I can specify the input date format?
On an other forum I found : date -d "${OLD_DATE}" -D "%d%m%Y" +%F
where -D should specify the input format but this doesn't work. But -D is unknown.
thanks for the help and sorry for my English.

You could to it like this:
echo "DDMMYYYY" | awk 'BEGIN {OFS="-"} {print substr($1,5,4), substr($1,3,2), substr($1,1,2)}'
Output:
YYYY-MM-DD

Yes, date understands a lot of formats for -d, but when it's just 8 digits in a row, it interprets it as YYYYmmdd. I'm not sure if you can force it to read it differently, but in this case you can use a simple editor such as awk or sed instead:
$ OLD_DATE='08032011'
$ echo $OLD_DATE | sed -r 's/(.{2})(.{2})(.{4})/\3-\2-\1/'
2011-03-08
This will work on GNU sed. Note that it doesn't check the input (for brevity).

Related

Sed change not known string in one line leaving the rest untouched

I'm facing problem with using sed in my project. I have a file myfile.txt with content inside like this:
{"Argument_date": "2020-04-16", "Argument_post": "true", "Argument_like": "false"}
I need to change the date argument with actual date not touching the rest of the line - but this date can be any not just 2020-04-16 - so this is unknown string
So i did this:
sed 's/\"Argument_date\"\: \".*\"/\"Argument_date\"\: \"'"$(date +%Y-%m-%d)"'\"/g' myfile.txt
But I get output almost correct:
{"Argument_date": "2020-05-05"}
Sed is changing this unknown date correctly to new one from system date, but miss all the rest of the line.
Can someone please explain me what I'm doing wrong to get correct output like this:
{"Argument_date": "2020-05-05", "Argument_post": "true", "Argument_like": "false"}
Use jq to manipulate json:
jq --arg date "$(date +%Y-%m-%d)" '.Argument_date = $date'
Problem is with your regex, it matches the whole string and then replaces it with "Argument_date": "2020-05-05"
Try changing your regex to match only the required part. One solution could be:
sed -E 's/(.*?)"Argument_date":\s+"[0-9]+-[0-9]+-[0-9]+"/\1"Argument_date":"'"$(date +%Y-%m-%d)"'"/g' file1

change format from %Y%m%d%H%M% to another format

how can I change date format from "%Y%m%d%H%M" to "%Y-%m-%d" "%H:%M" in shell scripting?
there is a log file (logfile.txt) with date format as "%Y%m%d%H%M%S" in first column. what is needed is print first column like "%Y-%m-%d" "%H:%M".
e.g: from 201804041323 to 2018-04-04 13:23
Thanks in advance :)
perl -i.bak -pe 's/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/$1-$2-$3 $4:$5/' file
sed -r 's/^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/\1-\2-\3 \4:\5/g' logfile.txt > newlogfile.txt
You can use below command for getting output in format of "2018-04-18 22:14"
date +%Y-%m-%d" "%H:%M

convert string ddmmyyyy to yymmdd using linux

I am struggling to convert a textfile which contains the string:
10.04.2015 12:00:15
to
150410
The date will always be the date format but is taken from a textfile so as far as I know I can't use a date command and I think I must use awk but I don't know it well and am struggling - advice would be appreciated?
You can use bash parameter expansion to extract substrings
$ date="10.04.2015 12:00:15"
$ newdate=${date:8:2}${date:3:2}${date:0:2}
$ echo $newdate
150410
The following sed command should be good enough:
sed -r 's/([0-9]+)\.([0-9]+)\.[0-9]{2}([0-9]{2}) ([0-9]+:?){3}/\3\2\1/g' input
or awk:
awk -F'[.: ]' '{print substr($3,3),$2,$1}' OFS='' input
After all I prefer the latter.
alternative answer, using awk:
DATE="10.04.2015 12:00:15"
echo $DATE | awk '{print substr($0,9,2) substr($0,4,2) substr($0,1,2)}'
Some explanation if you want to do modifications later:
$0 is the input text on which substr operates
the the first digit is where the substring starts
the second digit indicates the length of the substring

Command Line Date Formatting Assistance

I am running a report for my boss, and there were dates showing as DD/MM/YYYY (example: 18/06/2013). What can I use in my command to change this to MM/DD/YYYY? I'm thinking SED but I am not sure.
Also, the date is the first element in the log I'm looking at.
$ echo 18/06/2013 | sed -r 's|(..)/(..)/|\2/\1/|'
06/18/2013

Change date format in first column using awk/sed

I have a shell script which is automatically ran each morning which appends that days results to a text file. The file should have todays date on the first column followed by results separated by commas. I use the command date +%x to get the day in the required format (dd/mm/yy). However on one computer date +%x returns mm/dd/yyyy ( any idea why this is the case?). I then sort the data in the file in date order.
Here is a snippet of such a text file
29/11/12,9654.80,194.32,2.01,7.19,-7.89,7.65,7.57,3.98,9625.27,160.10,1.66,4.90,-4.79,6.83,4.84,3.54
03/12/12,5184.22,104.63,2.02,6.88,-6.49,7.87,6.67,4.10,5169.52,93.81,1.81,5.29,-5.45,7.87,5.37,4.10
04/12/12,5183.65,103.18,1.99,6.49,-6.80,8.40,6.66,4.38,5166.04,95.44,1.85,6.04,-6.49,8.40,6.28,4.38
11/07/2012,5183.65,102.15,1.97,6.78,-6.36,8.92,6.56,4.67,5169.48,96.67,1.87,5.56,-6.10,8.92,5.85,4.67
07/11/2012,5179.39,115.57,2.23,7.64,-6.61,8.83,7.09,4.62,5150.17,103.52,2.01,7.01,-6.08,8.16,6.51,4.26
11/26/2012,5182.66,103.30,1.99,7.07,-5.76,7.38,6.37,3.83,5162.81,95.47,1.85,6.34,-5.40,6.65,5.84,3.44
11/30/2012,5180.82,95.19,1.84,6.51,-5.40,7.91,5.92,4.12,5163.98,91.82,1.78,5.58,-5.07,7.05,5.31,3.65
Is it possible to change the date format for the latter four lines to the correct date format using awk or sed? I only wish to change the date format for those in the form mm/dd/yyyy to dd/mm/yy.
It looks like you're using two different flavors (versions) of date. To check which versions you've got, I think GNU date accepts the --version flag whereas other versions, like BSD/OSX will not accept this flag.
Since you may be using completely different systems, it's probably safest to avoid date completely and use perl to print the current date:
perl -MPOSIX -e 'print POSIX::strftime("%d/%m/%y", localtime) . "\n"'
If you are sure you have GNU awk on both machines, you could use it like this:
awk 'BEGIN { print strftime("%d/%m/%y") }'
To fix the file you've got, here's my take using GNU awk:
awk '{ print gensub(/^(..\/)(..\/)..(..,)/, "\\2\\1\\3", "g"); next }1' file
Or using sed:
sed 's/^\(..\/\)\(..\/\)..\(..,\)/\2\1\3/' file
Results:
29/11/12,9654.80,194.32,2.01,7.19,-7.89,7.65,7.57,3.98,9625.27,160.10,1.66,4.90,-4.79,6.83,4.84,3.54
03/12/12,5184.22,104.63,2.02,6.88,-6.49,7.87,6.67,4.10,5169.52,93.81,1.81,5.29,-5.45,7.87,5.37,4.10
04/12/12,5183.65,103.18,1.99,6.49,-6.80,8.40,6.66,4.38,5166.04,95.44,1.85,6.04,-6.49,8.40,6.28,4.38
07/11/12,5183.65,102.15,1.97,6.78,-6.36,8.92,6.56,4.67,5169.48,96.67,1.87,5.56,-6.10,8.92,5.85,4.67
11/07/12,5179.39,115.57,2.23,7.64,-6.61,8.83,7.09,4.62,5150.17,103.52,2.01,7.01,-6.08,8.16,6.51,4.26
26/11/12,5182.66,103.30,1.99,7.07,-5.76,7.38,6.37,3.83,5162.81,95.47,1.85,6.34,-5.40,6.65,5.84,3.44
30/11/12,5180.82,95.19,1.84,6.51,-5.40,7.91,5.92,4.12,5163.98,91.82,1.78,5.58,-5.07,7.05,5.31,3.65
This should work: sed -re 's/^([0-9][0-9])\/([0-9][0-9])\/[0-9][0-9]([0-9][0-9])(.*)$/\2\/\1\/\3\4/'
It can be made smaller but I made it so it would be more obvious what it does (4 groups, just switching month/day and removing first two chars of the year).
Tip: If you don't want to cat the file you could to the changes in place with sed -i. But be careful if you put a faulty expression in you might end up breaking your source file.
NOTE: This assumes that IF the year is specified with 4 digits, the month/day is reversed.
This below command will do it.
Note:No matter how many number of lines are present in the file.this will just change the last 4 lines.
tail -r your_file| awk -F, 'NR<5{split($1,a,"/");$1=a[2]"/"a[1]"/"a[3];print}1'|tail -r
Well i could figure out some way without using pipes and using a single awk statement and this solution does need a tail command:
awk -F, 'BEGIN{cmd="wc -l your_file";while (cmd|getline tmp);split(tmp,x)}x[1]-NR<=4{split($1,a,"/");$1=a[2]"/"a[1]"/"a[3];print}1' your_file
Another solution:
awk -F/ 'NR<4;NR>3{a=$1;$1=$2;$2=a; print $1"/"$2"/" substr($3,3,2) substr($3,5)}' file
Using awk:
$ awk -F/ 'NR>3{x=$1;$1=$2;$2=x}1' OFS="/" file
By using the / as the delimiter, all you need to do is swap the 1st and 2nd fields which is done here using a temporary variable.

Resources