Skip to main content
added 333 characters in body
Source Link
Jellicle
  • 420
  • 1
  • 6
  • 18

Substitution expressions in sed support a numeric flag. Here's how GNU sed can do it:

sed 's/:/-/g;s/-/:/3g'

So what's going on here? This expression has two parts, separated by ;. Here they are:

  1. First, replace all colons with hypens
  2. Then replace all hyphens except the first 2 with colons

This isn't generally useful in cases where your initial string has hyphens or colons that could interfere, but it does work on your date-string example.

Re. exiftool

That said, I wonder if you're getting your colon-formatted date from exiftool because I've seen that as its default date format. With exiftool, you can just specify a different date format:

exiftool -d '%Y-%m-%d %H:%M:%S' some-input-file.jpg

When you mix sed's g and number modifiers

Note: the POSIX standard does not specify what should happen when you mix the g and number modifiers, and currently there is no widely agreed upon meaning across sed implementations. For GNU sed, the interaction is defined to be: ignore matches before the numberth, and then match and replace all matches from the numberth on. [source]

Substitution expressions in sed support a numeric flag. Here's how GNU sed can do it:

sed 's/:/-/g;s/-/:/3g'

So what's going on here? This expression has two parts, separated by ;. Here they are:

  1. First, replace all colons with hypens
  2. Then replace all hyphens except the first 2 with colons

This isn't generally useful in cases where your initial string has hyphens or colons that could interfere, but it does work on your date-string example.

Note: the POSIX standard does not specify what should happen when you mix the g and number modifiers, and currently there is no widely agreed upon meaning across sed implementations. For GNU sed, the interaction is defined to be: ignore matches before the numberth, and then match and replace all matches from the numberth on. [source]

Substitution expressions in sed support a numeric flag. Here's how GNU sed can do it:

sed 's/:/-/g;s/-/:/3g'

So what's going on here? This expression has two parts, separated by ;. Here they are:

  1. First, replace all colons with hypens
  2. Then replace all hyphens except the first 2 with colons

This isn't generally useful in cases where your initial string has hyphens or colons that could interfere, but it does work on your date-string example.

Re. exiftool

That said, I wonder if you're getting your colon-formatted date from exiftool because I've seen that as its default date format. With exiftool, you can just specify a different date format:

exiftool -d '%Y-%m-%d %H:%M:%S' some-input-file.jpg

When you mix sed's g and number modifiers

Note: the POSIX standard does not specify what should happen when you mix the g and number modifiers, and currently there is no widely agreed upon meaning across sed implementations. For GNU sed, the interaction is defined to be: ignore matches before the numberth, and then match and replace all matches from the numberth on. [source]

added 33 characters in body
Source Link
Jellicle
  • 420
  • 1
  • 6
  • 18

Substitution expressions in sed support a numeric flag. Here's how GNU sed can do it:

sed 's/:/-/g;s/-/:/3g'

So what's going on here?

I'm on Ubuntu 20, and sed substitution expresses support a numeric suffix. This expression has two parts, separated by ;. Here they are:

  1. First, replace all colons with hypens
  2. Then replace all hyphens except the first 2 with colons

This isn't generally useful in cases where your initial string has hyphens or colons that could interfere, but it does work on your date-string example.

Note: the POSIX standard does not specify what should happen when you mix the g and number modifiers, and currently there is no widely agreed upon meaning across sed implementations. For GNU sed, the interaction is defined to be: ignore matches before the numberth, and then match and replace all matches from the numberth on. [source]

sed 's/:/-/g;s/-/:/3g'

So what's going on here?

I'm on Ubuntu 20, and sed substitution expresses support a numeric suffix. This expression has two parts:

  1. First, replace all colons with hypens
  2. Then replace all hyphens except the first 2 with colons

This isn't generally useful in cases where your initial string has hyphens or colons that could interfere, but it does work on your date-string example.

Substitution expressions in sed support a numeric flag. Here's how GNU sed can do it:

sed 's/:/-/g;s/-/:/3g'

So what's going on here? This expression has two parts, separated by ;. Here they are:

  1. First, replace all colons with hypens
  2. Then replace all hyphens except the first 2 with colons

This isn't generally useful in cases where your initial string has hyphens or colons that could interfere, but it does work on your date-string example.

Note: the POSIX standard does not specify what should happen when you mix the g and number modifiers, and currently there is no widely agreed upon meaning across sed implementations. For GNU sed, the interaction is defined to be: ignore matches before the numberth, and then match and replace all matches from the numberth on. [source]

Source Link
Jellicle
  • 420
  • 1
  • 6
  • 18

sed 's/:/-/g;s/-/:/3g'

So what's going on here?

I'm on Ubuntu 20, and sed substitution expresses support a numeric suffix. This expression has two parts:

  1. First, replace all colons with hypens
  2. Then replace all hyphens except the first 2 with colons

This isn't generally useful in cases where your initial string has hyphens or colons that could interfere, but it does work on your date-string example.