2

I want a batch script to insert comma left to last but one word and between last but one word and last word in all lines of a file. The commas should replace the spaces between the words.

E.g. test file:

This is first line
This is the second line
Check Subsystem version 3.1.8-11P

I want output look like:

This is,first,line
This is the,second,line
Check Subsystem,version,3.1.8-11P

This script is inserting comma before the last word, but I have a line in between with & and its deleting that line with & symbol in it.

E.g. Create & Delete Version 1.1.1

This line is getting deleted.

@echo off
for /f usebackq^ delims^=^ eol^= %%A in ("Doc.txt") do (
    set "s=%%A"
    set "s1=%%A"
    setlocal enableDelayedExpansion
    set "s=!s:@=@a!"
    set "s=!s:\=@b!"
    set "s=!s:/=@f!"
    set "s=!s:.=@d!"
    set "s=!s: =.!"
    for /f "delims=" %%A in (".!s!") do (
        endlocal
        set "s=%%~xA"
        setlocal enableDelayedExpansion
        if defined s (
            set "s=!s:.= !"
            set "s=!s:@d=.!"
            set "s=!s:@f=/!"
            set "s=!s:@b=\!"
            set "s=!s:@a=@!"
            set "s=!s:~1!"
            call set s2=%%s1:!s!=%%
            echo !s2!,!s! >> output.txt
        )
        endlocal
    )
)
pause

And also I'm not sure how to insert comma left to last but one word.

Deepti
  • 33
  • 4
  • 1
    Where is the Windows batch file you require us to help you with? There's a difference between helping somebody you and doing it for them, as it currently stands your request is the latter and is therefore off topic here. – Compo Oct 17 '17 at 10:51

1 Answers1

1

This can be done easily with any text editor supporting regular expressions by searching for
\W+(\w+)\W+(\w+)$ and using as replace string ,$1,$2.

How can you find and replace text in a file using the Windows command-line environment? contains in one answer the link to latest version of JREPL.BAT written by by Dave Benham which makes it possible to make this modification on the file using the regular expression search and replace strings.

For usage of JREPL.BAT from within a batch file with this additional batch file in same directory as the executed batch file:

call "%~dp0jrepl.bat" "\W+(\w+)\W+(\w+)$" ",$1,$2" /F "%~1" /O -

"%~1" references here in this generic command line the name of the file to modify.

Run jrepl.bat /? for help on the other options used here.

Run call /? for help on this command and explanation of %~dp0 (drive and path of current batch file ending with a backslash).

Explanation of the regular expression replace:

  • \W+ ... find 1 or more non word character according to Unicode standard.
  • (\w+) ... find 1 or more word character according to Unicode standard and mark the found string for back-referencing in replace string with $1.
  • \W+ ... find again 1 or more non word character according to Unicode standard.
  • (\w+) ... find again 1 or more word character according to Unicode standard and mark the found string for back-referencing in replace string with $2.
  • $ ... search expression results in a positive match only if the two words could be found at end of a line whereby the newline characters are not matched.

For a line like

Check Subsystem version 3.1.8-11P

which should be modified to

Check Subsystem,version,3.1.8-11P

in the file a different regular expression search string is necessary because of . and - are non word characters according to Unicode standard.

The search string must be modified to: [^\w.\-]+([\w.\-]+)[^\w.\-]+([\w.\-]+)$

[...] is positive character class definition matching any character specified within the square brackets.

[^...] is a negative character class definition matching any character not specified within the square brackets.

\w is a special character class definition for all word characters according to Unicode standard.

. inside square brackets of a positive or negative character class definition is interpreted as literal character. The dot character would have a special meaning in regular expression search strings outside a character class definition.

- inside square brackets of a positive or negative character class definition means all characters from character X specified left to - to character Y specified right to - according to code values of the characters X and Y. The hyphen is interpreted also as literal character if there is no character either left or right to - inside the character class definition. However, it is advisable to escape - with a backslash inside a character class definition independent on its position within the brackets when the hyphen character should be interpreted as literal character. Outside a character class definition the hyphen character has no special meaning.

So the entire command line to use for a file containing strings with dot and hyphen which should be interpreted as "words" is:

call "%~dp0jrepl.bat" "[^\w.\-]+([\w.\-]+)[^\w.\-]+([\w.\-]+)$" ",$1,$2" /F "%~1" /O -
Mofi
  • 38,783
  • 14
  • 62
  • 115