Solved Batch script to remove columns from a text file

March 30, 2015 at 08:24:06
Specs: Windows 7
I have a text file with hundreds of rows and I want to use a batch script to extract 3 columns from it and input the three columns into a new text file. I always need the first and last column and the phone number. Sometimes the phone number is in the 4,5,6, columns and sometimes it has dashes.

Text file example:
31 David Glass 213-842-1960 PT KLZ:32
55 Frank Henry Simms 213-842-1959 NX P K:L
G1 Peter Aaron John Henry 3605152333 G L :7

message edited by Triskal


See More: Batch script to remove columns from a text file

Report •

#1
March 31, 2015 at 01:06:23
✔ Best Answer
:: lesson learned: goto :gotP broke the script 31 March 2015 12:49
:: extracts first last & phone number
::======  script starts here  ===============

@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims= " %%a in (myfile) do (
call :sub1 %%a
) >> NEWFILE
goto :eof

:sub1
set N=
set P=
for %%i in (%*) do (
:loop1
set/a N+=1
set C!N!=%1

    if !N! gtr 1 if not defined P (
      set S=%1
      set F=!S:~0,1!
      for /L %%x in (0 1 9) do (
        if !F! equ %%x (
          set P=!S!
        )
      )
    )

  if '%2' neq '' (
    SHIFT
    goto :loop1
  )
)

set/a T=N-2
echo !C1! !P! !C%N%!

goto :eof
::======  script ends here  =================

myfile:

31 David Glass 213-842-1960 PT KLZ:32
55 Frank Henry Simms 213-842-1959 NX P K:L
G1 Peter Aaron John Henry 3605152333 G L :7

=====================

M2 Get custom script or take private lessons


Report •

#2
April 1, 2015 at 08:57:07
https://en.wikipedia.org/wiki/File:...

Thank you Mechanix2Go! Worked like a charm.

Now you have my mind singing "Gotta give you love oh Cherry...and... Easy, easy on my soul, I said easy, easy on my soul..." for the past 2 days!


Report •

#3
April 1, 2015 at 20:33:59
You're the first person to get the pun.

Or maybe just the first to react.

=====================

M2 Get custom script or take private lessons


Report •
Related Solutions


Ask Question