How to match date mm/dd/yyyy - sed

Given the following input
506028500,1820196768,43,0,84552257, ,1,-71.16,42.42,04/26/2012,
I want to filter the date at the end of the row, so I can change its mm/dd/yyyy format into MySQL date format yyyy-mm-dd:
I am trying to match the date at the end of the row having first removed the trailing ',' (comma).
{
s/,$//g
/\([0-9][0-9]\)\/\([0-9][0-9]\)\/\([0-9][0-9][0-9][0-9]\)$/=
}
I'm getting 12847, but am expecting 04262012.
What am I doing wrong, that I can't even match the date that's there?
Thanks.

This might work for you:
echo "506028500,1820196768,43,0,84552257, ,1,-71.16,42.42,04/26/2012," |
sed 's|,\(..\)/\(..\)/\(....\),$|,\3-\2-\1,|'
506028500,1820196768,43,0,84552257, ,1,-71.16,42.42,2012-26-04,
If you just want the date:
echo "506028500,1820196768,43,0,84552257, ,1,-71.16,42.42,04/26/2012," |
sed 's|.*,\(..\)/\(..\)/\(....\),$|\3-\2-\1|'
2012-26-04

This works for me:
sed "s/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$/\3-\1-\2/g" someFile.txt
At least it replaces the date string with one of the desired format.
To only print the date at the end, the following works for me:
sed "s/.*\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$/\3-\1-\2/g" someFile.txt

Related

Powershell CSV modification

CSV source exemple:
"Requested Quantity","Unit of Measure","Requested Date","Forecast Timing Qualifier","Rating"
"4.00 ","EA","26.09.2022","W","DOA1"
"12.00 ","EA","27.09.2022","W","DOA1"
I need to remove the last 3 Characters to Requested Quantity and
I need to convert the Requested Date format to Month.Day.Year for each line.
Input : "4.00 ","EA","26.09.2022","W","DOA1"
Output : "4","EA","09/26/2022","W","DOA1"
I got the Requested quantity ok but I need some help with converting the date portion.
Thanks
For the Requested Quantity column you can use the -replace operator to remove the dot and everything after. For the date column, use [Datetime]::ParseExact() to parse the existing format, then convert back to a string with the desired format:
Import-Csv path\to\source.csv |ForEach-Object {
# Cut the trailing decimals off the quantity
$_.'Requested Quantity' = $_.'Requested Quantity' -replace '\..*$'
# Parse and re-format date column
$_.'Requested Date' = [datetime]::ParseExact($_.'Requested Date', 'dd.MM.yyyy', $null).ToString('MM/dd/yyyy')
# output the modified object for export
$_
} |Export-Csv path\to\output.csv -NoTypeInformation

Regular Expression for date extraction

I have a file that has a name: Yonder_CompetitionEntries_20210928080000, I want to extract 20210928. Basically the year, month and day.
So far I have this and it's not working. The file has an extension of csv.gz
date_key = """RIGHT(regexp_replace(regexp_replace(filename,'.gz',''),'.csv',''), 8)"""
what about without regex?
We split on _ and take the last element, then we parse get the first few elements of the string.
date_key = """substring(element_at(split(filename, '_'), -1), 1, 8)"""

Firefox Bookmarks add_date

One question to firefox exported bookmarks.
How to read this date: ADD_DATE="1375088003"? I think it is date when user add new website to bookmarks but how to read this all string? Where is year, day and month?
It's the Epoch time, ie the number of seconds since Jan 1 1970. To convert it to a human-friendly date, you can pass it through the 'date' unix command:
date -r 1375088003
will return
Mon 29 Jul 2013 18:53:23 AEST
(on my trusty Mac here in eastern Australia, hence the AEST)
Here's what I do. I export bookmarks from my browser. Assume the result file is called "bookmarks.html". I have a one-line executable script that takes a file as input, and creates another file with many long fields stripped. The one-liner I call "icon.sed" contains this:
sed 's/ICON=\"[^\"]*\"//' | sed 's/ICON_URI=\"[^\"]*\"//' | \
sed 's/LAST_CHARSET=\"[^\"]*\"//' | sed 's/HREF=\"[^\"]*\"//'| \
tr -s ' '
I use it like this:
~/icon.sed <bookmarks.html >newbook.txt
I have another script that takes a mm/dd/ccyy parameter, and returns the Epoch time for that date. That executable script is called "epoch.sh":
#!/bin/sh
if [ -z "$1" ]; then echo "You must supply a date"
else date -j -f "%m/%d/%Y" "$1" "+%s"; fi
exit 0
Choose a date you're looking for, and send it through "epoch.sh", such as: ~/epoch.sh 10/30/2015 which outputs this: 1446246082 .
You can then grep your newbook.txt file looking for either ADD_DATE or LAST_MODIFIED date which begins with the first four digits: 1446
grep 'LAST_MODIFIED="1446' newbook.txt
Also, use epoch.sh to get a pair of epoch times for start and stop dates so you can bracket your results by doing multiple greps over the range, using just the first four digits of each epoch time.
To convert a Firefox timestamp to a date with BASIC:
If the timestamp is from an sqlite dump of places.sqlite, you need to truncate it first:
stg0 = Left("1670163289502000", 10) results in "1670163289"
Convert the timestamp:
Dim var0 As double, var1 As Date
var0 = stg0
or
var0 = "1670163289" 'BASIC will do the conversion for you
var1 = (var0 / 86400) + 25569
where 86400 is the # of seconds in a day and 25569 is the BASIC daynumber for 1/1/1970

How can reorder a file by asc or desc order from specific column

How can reorder a file by asc or desc order from specific column date and time or only date column.
{"hostname":"sg2","role":"epay","eventid":"ALERT_ORACLE","date":"02/08/2011","time":"01:30:00"},
{"hostname":"sg1","role":"pay","eventid":"ALERT_DF","date":"11/24/2010","time":"19:33:01"},
{"hostname":"sg3","role":"ipay","eventid":"ALERT_SMF","date":"12/11/2012","time":"02:30:00"},
Usually use "sort -gk (column number)" in that case not able to catch date and time column or only date, maybe is need some separator string or related.
Expetected view is:
{"hostname":"sg1","role":"pay","eventid":"ALERT_DF","date":"11/24/2010","time":"19:33:01"},
{"hostname":"sg2","role":"epay","eventid":"ALERT_ORACLE","date":"02/08/2011","time":"01:30:00"},
{"hostname":"sg3","role":"ipay","eventid":"ALERT_SMF","date":"12/11/2012","time":"02:30:00"},
Your life would be easier if you had chosen an ISO date format (YYYY-mm-dd), but
sort_by_date() {
awk -F \" '{
split($16, date, "/")
split($20, time, ":")
timestamp = mktime(date[3]" "date[1]" "date[2]" "time[1]" "time[2]" "time[3])
print timestamp "\t" $0
}' "$1" |
sort -k1,1n |
cut -f2-
}
sort_by_date filename
For descending, use sort -k1,1nr
Use sort with : as delimiter and order by column 5M for date comparison:
$ sort -t: -k5M file
{"hostname":"sg1","role":"pay","eventid":"ALERT_DF","date":"11/24/2010","time":"19:33:01"},
{"hostname":"sg2","role":"epay","eventid":"ALERT_ORACLE","date":"02/08/2011","time":"01:30:00"},
{"hostname":"sg3","role":"ipay","eventid":"ALERT_SMF","date":"12/11/2012","time":"02:30:00"},
Source: Sort logs by date field in bash.

How to write expression to convert yyyymm to mm-yyyy in ssrs?

I have a table with a YearMonth column (201302) in it.
I created YearMonth a parameter.
Now I would like to write an expression in SSRS so that every time the report runs the date and month would look like this Feb-2013.
Could you please suggest me why following expression did not work.
Thanks
=CStr(Right(MonthName(Month(Parameters!NLR_YearMonth.Value)),3))
+ "-" +
Left(Year(Parameters!NLR_YearMonth.Value),4)
Try This:
=Format(DateValue(MonthName(Right(Parameters!NLR_YearMonth.Value, 2))
+ "," +
Left(Parameters!NLR_YearMonth.Value,4)), "Y")
The expression goes as follows:
Cutting the string to get the month.
Converting the mount to MountName
Creating a comma seperator between the month and the year
Cutting the string to get the year
Converting the returns string to Date object using the DateValue function
Formatting the Date to fits your needs
The results should be:
February, 2013