Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed.exe -e expression #1, unknown command

Tags:

linux

windows

sed

First of all, sorry if it is duplicated but I am a totally newbie on Sed. I was assigned to investigate an error log but after fixing it based on some tutorials my problem is still not solved.

Here is the original code:

@sed.exe -i "s/\(.*CONFIG_PROJECT_SW_DATE_VERSION=\"\).*\(\"\)/\1%date:~2,2%%date:~5,2%%date:~8,2%\2/g/" .\ghsproj_du2_avc_src.gpj

after running, I received the following error:

03-27 13:22:43 sed.exe: -e expression #1, char 53: unknown option to `s'
03-27 13:22:43 Could Not Find C:\jenkins-slave\workspace\project\work\build\ghs\sed*

I have read some solutions like remove the blank space between -i and s command or enter a "/" at the end but nothing works so far.

P/S: I ran this on Windows 10 environment.

like image 418
Kyoshin Avatar asked Dec 07 '25 06:12

Kyoshin


1 Answers

Try this (GNU sed):

for /f "tokens=2 delims==" %a in ('wmic OS Get localdatetime /value') do set "dt=%a"
sed.exe "s@\(.*CONFIG_PROJECT_SW_DATE_VERSION=\x22\).*\(\x22\)@\1%dt:~2,2%%dt:~4,2%%dt:~6,2%\2@g" file

Change the two %a to %%a if you are putting them in a batch file.
I removed -i switch, add it back when you tested that the output is okay.

Several things to be considered:

  • You are using sed on cmd or in batch file, thus be careful about the double quotes and the escaping. So I just changed the quotes inside with \x22 which is another way to refer to characters by their ASCII code. (GNU sed feature).
  • On cmd or in batch file, %date:~2,2%%date:~5,2%%date:~8,2% does not guaranteed to be YYMMDD, depending on different Locale settings, they can have / inside, maybe it's the cause of your error. I tried another way to get the date, copied the idea from this answer.
  • You can change the delimiter of s command to other character, I changed to @ here.
  • There should be no / after the g flag.

Example:

> type file
var CONFIG_PROJECT_SW_DATE_VERSION="whatever"

> @for /f "tokens=2 delims==" %a in ('wmic OS Get localdatetime /value') do @set "dt=%a"
> sed.exe "s@\(.*CONFIG_PROJECT_SW_DATE_VERSION=\x22\).*\(\x22\)@\1%dt:~2,2%%dt:~4,2%%dt:~6,2%\2@g" file
var CONFIG_PROJECT_SW_DATE_VERSION="190327"
like image 179
Tiw Avatar answered Dec 08 '25 21:12

Tiw