3

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.

Kyoshin
  • 57
  • 5
  • 1
    The slash after the `g` modifier is a mistake — possibly even _the_ mistake. Removing the space between `-i` and the `s///g` command would not help; it would confuse things even more. – Jonathan Leffler Mar 27 '19 at 06:06
  • @JonathanLeffler Thanks for the answer, I have edited the code and removed the backslash but it would return another error: sed.exe: -e expression #1, char 1: unknown command: `.' – Kyoshin Mar 27 '19 at 06:13
  • Character position 53 is the `%` at the start of `%date:~2,2%`, which is presumably a variable substitution in DOS `cmd` scripting. Do `echo %data:~2,2%` (or its equivalent — show the result of the expansion somehow) and there will (almost certainly) be a slash in the result, and that slash is throwing your command off. Adjust the numbers accordingly; test the separate `%date:…%` bits first before building them into the `sed` command. – Jonathan Leffler Mar 27 '19 at 06:18
  • Note that you currently have `"s/…/…/g/"`, even after your edit. You need `"s/…/…/g"` only. Those are slashes, not backslashes. But I'm fairly confident that your date substring operation is what's causing you grief. You could also try simply echoing the command as a whole to see what `sed` is being passed. – Jonathan Leffler Mar 27 '19 at 06:23

1 Answers1

1

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"
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Til
  • 5,150
  • 13
  • 26
  • 34