No Manual Entry

My Everything Howtos

It's never a good idea to ask an engineer a question in the 'why don't we' format. (Dilbert)

0 notes

Dates in bash?

Milliseconds (say Java System.currentTimeInMillis()) in bash

$ date +%s%3N

Date plus/minus years, months, weeks, days, hours, minutes, seconds,…      

Past

$ date --date="1 week ago" # or "-7 day"
$ date --date="1 day ago" # or "1 days ago" or "-1 day" or "yesterday"
$ date --date "1 year ago 6 months ago"

Future

$ date --date="3 weeks" # or "21 day"
$ date --date="1 year" # or "next year"
$ date --date="next Friday" # or "next Fri"
$ date --date="last Friday" # or "last Fri"

Convert seconds since the epoch (1970-01-01 UTC) to a date

$ date --date='@1395325505'

Days between two dates

$ echo $(( (`date --date "6 day" +%s` - `date --date "1 month ago" +%s`) / 60 / 60 / 24 ))

See more: http://www.cyberciti.biz/tips/linux-unix-get-yesterdays-tomorrows-date.html

Filed under bash date milliseconds addition substraction java currentTimeInMillis System.currentTimeInMillis

0 notes

Normalize accented characters in Java

In order to convert

Això és il·lustratiu, dançant amb paraigües

to

Aixo es illustratiu, dancant amb paraigues

just use

import java.text.*;

String str1 = "Això és il·lustratiu, dançant amb paraigües";

String str2 = Normalizer.normalize(str1, Normalizer.Form.NFKD).replaceAll( “[^\\x20-\\x7E]”, ”“ );

In jEdit you can search and replace using this snippet thanks to the BeanShell replacement mode. For example, search:

(.*)

and replace with the text value of the next BeanShell snippet:

java.text.Normalizer.normalize( _1, java.text.Normalizer.Form.NFKD ).replaceAll( "[^\\\\x20-\\\\x7E]\", "" )

Filed under normalize accented characters Java jEdit BeanShell programming search and replace

2 notes

Search and replace repeated patterns

Suppose you have something like: 

abc - img102.jpg
abc - img134.jpg
abc - img332.jpg
defghi - img001.jpg
defghi - img122.jpg
defghi - img943.jpg
defghi - img234.jpg
defghi - img993.jpg
jklm - img544.jpg
jklm - img222.jpg

and want it to be:

"imgs" : [
   ”abc - img102.jpg”,
   ”abc - img134.jpg”,
   ”abc - img332.jpg”
]
   
“imgs” : [
   ”defghi - img001.jpg”,
   ”defghi - img122.jpg”,
   ”defghi - img943.jpg”,
   ”defghi - img234.jpg”,
   ”defghi - img993.jpg”
]

“imgs” : [
   ”jklm - img544.jpg”,
   ”jklm - img222.jpg”
]

Start searching for:

(.+)( - img.*\n)((\1 - img.*\n)+)

and replace with:

"imgs" : [\n$1$2$3]\n

To add quotes and commas search for:

(.+ - img.*)\n

and replace with:

\t”$1”,\n

And to clean last comma before closing bracket search for:

,(\n\])

and replace with:

$1

As to the non repeated lines, search for:

(\]\n)(\t”)(.*),

and replace with:

$1”imgs” : [\n$2$3\n]

Filed under programming regexp regular expressions repeated patterns search and replace