1

I have a file (Flags.txt) that looks like this:

...

C_INCLUDES = ... ... .../xxx
...

CXX_INCLUDES = ... ... .../yyy

where the line with C_INCLUDES can end with any string (here e.g xxx).

At the end, the file shall look like this:

...

C_INCLUDES = ... ... .../xxx/

...

CXX_INCLUDES = ... ... .../yyy

Therefore I want to use a windows batch file (not possible to use sed or awk) to search for the name C_INCLUDES and append at the end of the line the forward slash (but could be any smbol e.g "xxxz" or "xxx!" )?

I tried the solution from:

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/fa09e27d-9f6b-4d4e-adda-f0663e0a9dde/append-string-to-text-file-at-end-of-line-starting-with-blah?forum=ITCG

$original = "flags.txt"
$tempfile = "tmp.txt"

get-content $original | foreach-object {
  if ($_ -match "^C_INCLUDES") {
    $_ + "/" >> $tempfile
  }
  else {
    $_ >> $tempfile
  }
}

copy-item $tempfile $original
remove-item $tempfile

But it don't work

Thanks

dbenham
  • 119,153
  • 25
  • 226
  • 353
ge45mue
  • 577
  • 3
  • 15
  • You do know that those are forward slashes don't you? please confirm which you want forward or back slashes. Please also add the code that you have tried. Make sure that you provide that information as an edit of your question not as a comment or answer to it. – Compo Nov 24 '16 at 11:52
  • 3
    The posted code from an answer on other website is a __PowerShell__ script code and __not__ command lines for a __batch file__. – Mofi Nov 24 '16 at 12:48
  • 2
    See the answers on [How can you find and replace text in a file using the Windows command-line environment?](http://stackoverflow.com/questions/60034/) Look especially on [answer written by dbenham](http://stackoverflow.com/a/16735079/3074564). `jrepl.bat` solution with a regular expression search and replace string can be used for this task. – Mofi Nov 24 '16 at 12:59
  • Thank you guys. I choose the solution from dbenham – ge45mue Nov 25 '16 at 08:58

1 Answers1

2

You imply you cannot use 3rd party (non-native) exe files such as sed. But you can use a batch file.

So you should have no problem using JREPL.BAT - a regular expression find/replace text processing utility. JREPL is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward - no 3rd party exe file required.

Full documentation is available from the command line via jrepl /?, or jrepl /?? for paged help.

Once you have JREPL.BAT, then the following one liner is all that is needed. It looks for any line that begins with C_INCLUDES and doesn't already end with /, and appends / to any line that matches.

jrepl "^C_INCLUDES .*(?=.$)[^/]" "$&/" /f "Flags.txt" /o -

Since JREPL is a batch script, you must use call jrepl if you put the command within another batch script.

dbenham
  • 119,153
  • 25
  • 226
  • 353