|
Answer» Well, I thought I was better at this than I am and I've got a problem I need some help with. I thought this would be simple, but at least right now, apparently I'm brain dead...
Here's the problem... I have a file called import.txt, the file layout looks like this...
4323 Name of COMPANY Light no no no no no no
4325 Name of next company #N/A no no no no no no
Each record is exactly the same size, seperated by a SPACE. Here's what I would like the batch file to do...
read import.txt Create a file from the first line of each record, i.e., 4323.txt insert the next eight lines exactly as shown move the the next record and do it all again until all the records are seperate files.
Thanks in advance for any help, I really appreciate it! Gary download gawk for WINDOWS, then use this one liner
Code: [Select]C:\test>gawk -vRS= -vFS="\n" "{print $0 > $1\".txt\"}" file
C:\test>more 4325.txt 4325 Name of next company #N/A no no no no no no
C:\test>more 4323.txt 4323 Name of Company Light no no no no no no I wanted to share the answer with anyone who might have been interested in this problem. It came from a different forum I'm a member of, from a guy named aGerman. Here's his reply...
-------------------------
It's not that easy. Maybe it will fail if the number of lines is greater than the 32bit LIMIT for numeric expressions in batch.
Code: echo off &setlocal set "empty=0" for /f "delims=: tokens=1*" %%a in ('findstr /n "^" "import.txt"') do ( if "%%b"=="" ( call set "empty=%%empty%% %%a" ) else ( set "line_%%a=%%b" ) )
for %%a in (%empty%) do ( set /a nameLine=%%a+1 set /a startLine=%%a+2 set /a endLine=%%a+9 call :proc ) goto :eof
:proc call set "name=%%line_%nameLine%%%.txt if "%name%"==".txt" goto :eof >"%name%" type nul for /l %%a in (%startLine%,1,%endLine%) do ( >>"%name%" call echo.%%line_%%a%% ) goto :eof
This code should work if the separator is an empty line. But you wrote the separator is a space. If this is realy true you have to replace:
Code: if "%%b"==" " (
-----------------------------
|