0

I want to replace American time to French time in PHP files from command line.

From date('m/d/Y \a\t h:i:s A' to date('Le d/m/Y à G:i:s'

I tried the following sed command :

sed -i "date('m/d/Y \a\t h:i:s A'&date('Le d/m/Y à G:i:s'&g" /path/file.php

It doesn't work. I think the problem comes from the antislashs in the source string \a\t

I already checked this question : sed command to replace string with slashes

I already tried to change the delimiters or escpae the anti-slashs with \\.

But nothing works. Any help is welcome.

potong
  • 55,640
  • 6
  • 51
  • 83
Fifi
  • 3,360
  • 2
  • 27
  • 53
  • Please add sample input (no descriptions, no images, no links) and your desired output for that sample input to your question (no comment). – Cyrus Sep 19 '21 at 08:28
  • @Cyrus It's already in my question : From `date('m/d/Y \a\t h:i:s A'` to `date('Le d/m/Y à G:i:s'` – Fifi Sep 19 '21 at 08:31

2 Answers2

1

Escape antislash like this :

sed -i "date('m/d/Y \\\\a\\\\t h:i:s A'&date('Le d/m/Y à G:i:s'&g" /path/file.php
Mr Robot
  • 1,037
  • 9
  • 17
0
  1. Unless you need double quotes for the shell to interpret something (e.g. expand variables) use single instead of double quotes around strings (including scripts) in shell so you don't have to add additional escapes and jump through other hoops to avoid the shell interpreting chars/strings in the script.
  2. Don't use metachars like & as script delimiters as at best it obfuscates your script, use a char that's always literal but doesn't appear in the script, e.g. # in this case.
  3. To include ' in a '-delimited sed script either use '\'' or \x27.
  4. I've never heard the term "antislash" in 40+ years of programming. The common term for the character \ is a backslash while / is a forwardslash.
  5. To include a backslash literally in a string it has to be escaped with an additional preceding backslash, i.e. \\ instead of just \.
$ cat file
foo date('m/d/Y \a\t h:i:s A' bar

$ sed 's#date('\''m/d/Y \\a\\t h:i:s A'\''#date('\''Le d/m/Y à G:i:s'\''#g' file
foo date('Le d/m/Y à G:i:s' bar

$ sed 's#date(\x27m/d/Y \\a\\t h:i:s A\x27#date(\x27Le d/m/Y à G:i:s\x27#g' file
foo date('Le d/m/Y à G:i:s' bar

Add the -i back when you've tested it does what you want.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185