<http://www.netikka.net/tsneti/info/tscmd035.htm>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Mon 23-Feb-2015 12:46:54

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



35} How can I substitute the second field on each line of my file?

Below is a simple demonstration substituting the second field with "ab" on all the lines. Note the formulation of the for tokens, so that all the fields will be covered.
  @echo off
  echo "123","45","678","901","234"> myfile.txt
  echo "234","56","789","012","345">>myfile.txt

  @echo off & setlocal enableextensions
  for /f "tokens=1,3,* delims=," %%q in ('type myfile.txt') do (
    echo %%q,"ab",%%r,%%s>>outfile.txt)
  type outfile.txt
  endlocal & goto :EOF

The output will be
  F:\CMD>d:cmdfaq
  "123","ab","678","901","234"
  "234","ab","789","012","345"

Note that the method will work even if the format of the lines were
"123" , "45" , "678" , "901" , "234"
For removing the quotes (if need be) and extracting separately the contents of each field, see the previous item.

With GnuWin32 gawk (let's call it unxgawk)
  unxgawk -F, "{printf \"%%s,\042ab\042,%%s,%%s,%%s\n\",$1,$3,$4,$5}" "myfile.txt"
The output will again be
  F:\CMD>d:cmdfaq
  "123","ab","678","901","234"
  "234","ab","789","012","345"
The solution assumes that all the lines contain the same number of fields. - The \042 is octal for the " quote character.

The original task can also be solved with a Visual Basic Script (VBScript) aided command line script. It is much more complicated and thus perhaps not practical. However, the code is instructive and allows for flexibility.
  @echo off & setlocal enableextensions
  ::
  :: Build a test file

  set testfile_=C:\_M\testfile.txt
  > "%testfile_%" echo "123","45","678","901","234"
  >>"%testfile_%" echo "999"
  >>"%testfile_%" echo "888","777"
  >>"%testfile_%" echo.
  >>"%testfile_%" echo "234","56","789","012","345"
  ::
  :: Build a Visual Basic Script

  set skip=
  set vbs_=%temp%\tmp$$$.vbs
  >"%vbs_%" findstr "'%skip%VBS" "%~f0"
  ::
  :: Run it with Microsoft Windows Script Host Version 5.6

  <"%testfile_%" cscript //nologo "%vbs_%"
  ::
  :: Clean up

  for %%f in ("%vbs_%" "%testfile_%") do if exist %%f del %%f
  endlocal & goto :EOF
  '
  '............................................
  ' The Visual Basic Script
  '

  TargetCol = 2 'VBS
  TargetCol = TargetCol - 1 'VBS
  Do While Not WScript.StdIn.AtEndOfStream 'VBS
    LineStr = WScript.StdIn.ReadLine 'VBS
    LineArray = Split(LineStr, ",", -1, vbTextCompare) 'VBS
    LastI = UBound(LineArray) 'VBS
    For i = 0 to LastI-1 'VBS
      If i = TargetCol Then 'VBS
        WScript.StdOut.Write Chr(34) + "ab" + Chr(34) 'VBS
        If LastI > TargetCol Then WScript.StdOut.Write "," 'VBS
      Else 'VBS
        WScript.StdOut.Write LineArray(i) + "," 'VBS
      End If 'VBS
    Next 'VBS
    If LastI > -1 Then 'VBS
      If LastI = TargetCol Then 'VBS
        WScript.StdOut.WriteLine Chr(34) + "ab" + Chr(34) 'VBS
      Else 'VBS
        WScript.StdOut.WriteLine LineArray(LastI) 'VBS
      End If 'VBS
    Else 'VBS
      WScript.StdOut.WriteLine 'VBS
    End If 'VBS
  Loop 'VBS

The output will be
  F:\CMD>d:cmdfaq
  "123","ab","678","901","234"
  "999"
  "888","ab"

  "234","ab","789","012","345"