Read the contents of txt file and rearrange.

Lenovo thinkcentre / Mt6088
May 28, 2010 at 01:39:54
Specs: Windows XP, 2GB
Hi, I'm new to batch files but I really need one to do this simple (repeatative task). I have a text file of data stored in multiple lines which are pretty long. I want to retain the first two words of a line and then subsequent words need to be grouped in four words and stored on new lines.

Thanks and Regards,
Om

Before:

12 11 22 34 55 35 21 56 76 93 22 12 34 12 45 12 00 65

76 89 98 54 78 28 75 21 09 53 78 98 04 29 80 61 65 32

After:

12 11
22 34 55 35
21 56 76 93
22 12 34 12
45 12 00 65

76 89
98 54 78 28
75 21 09 53
78 98 04 29
80 61 00 65


See More: Read the contents of txt file and rearrange.

Report •


#1
May 28, 2010 at 08:22:06
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=1-18 delims= " %%a in (mydata) do (
echo.%%a %%b
echo.%%c %%d %%e %%f
echo.%%g %%h %%i %%j
echo.%%k %%l %%m %%n
echo.%%o %%p %%q %%r
echo.
) >> newfile


=====================================
Helping others achieve escape felicity

M2


Report •

#2
May 30, 2010 at 21:42:56
This is pretty much what I was looking for, but this doesnt solve my problem altogether! In a single line i have 66 tokens and There are 2 such lines!

The solution you proposed only allows me to handle 26 tokens at max!

It will be great If you can suggest me a way to tackle this!
In the meanwhile I was going through some info posted here:

<http://stackoverflow.com/questions/980791/number-of-tokens-limit-in-a-for-command-in-a-windows-batch-script>

Thank You,
Om


Report •

#3
May 31, 2010 at 13:55:35
:: edited to make shorter version, tested ok up to 1000 tokens
@echo off & setlocal enabledelayedexpansion
for /f "tokens=1-2*" %%a in (mydata) do (
echo %%a %%b
call :xx %%c
)
goto :eof
:xx
if "%1" equ "" goto :eof
echo %1 %2 %3 %4
for %%z in (x x x x) do shift
goto :xx

Report •

Related Solutions

#4
May 31, 2010 at 17:56:00
"The solution you proposed only allows me to handle 26 tokens at max!"

Sure, but that's what you asked for.

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

@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=1-2* delims= " %%a in (mydata) do (
  call :sub1 %%c
  echo.%%a %%b
    for /L %%i in (1 1 !N!) do (echo.!V%%i!)
  echo.
) >> newfile

goto :eof

:sub1
set N=
  :loop
    set /a N+=1 & set V!N!=%1 %2 %3 %4
    if "%5" neq "" (
    for /L %%S in (1 1 4) do (shift)
    goto :loop
    )
goto :eof


=====================================
Helping others achieve escape felicity

M2


Report •

Ask Question