Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Please need help!! appending text to a certain position in each line of text file

37 views
Skip to first unread message

Kevin Lafontaine

unread,
Jun 26, 2017, 11:41:11 AM6/26/17
to
SO I have a text file that I need to append different text to alternating lines

here is the original test.txt file

SummaryEditCopy Master Control 5017062241090097 JKZ59708 ADS EVERYWHERE

SummaryEditCopy Master Control 5017062241090096 JKZ59708 ADS EVERYWHERE

SummaryEditCopy Master Control 5017062241090089 JKZ61088 ADS EVERYWHERE

SummaryEditCopy Master Control 5017062241090088 JKZ61088 ADS EVERYWHERE

SummaryEditCopy Master Control 2017062241090111 JGZ84982 ADS EVERYWHERE

SummaryEditCopy Master Control 2017062241090110 JGZ84982 ADS EVERYWHERE

SummaryEditCopy Master Control 2017062241090101 JGZ78633 ADS EVERYWHERE

SummaryEditCopy Master Control 2017062241090100 JGZ78633 ADS EVERYWHERE

SummaryEditCopy Master Control 2017062241090075 JWZ32568 ADS EVERYWHERE



**This is what I need it to look like (adding _SD and _HD alternating lines)


SummaryEditCopy Master Control 5017062241090097 JKZ59708_SD ADS EVERYWHERE

SummaryEditCopy Master Control 5017062241090096 JKZ59708_HD ADS EVERYWHERE

SummaryEditCopy Master Control 5017062241090089 JKZ61088_SD ADS EVERYWHERE

SummaryEditCopy Master Control 5017062241090088 JKZ61088_HD ADS EVERYWHERE

SummaryEditCopy Master Control 2017062241090111 JGZ84982_SD ADS EVERYWHERE

SummaryEditCopy Master Control 2017062241090110 JGZ84982_HD ADS EVERYWHERE

SummaryEditCopy Master Control 2017062241090101 JGZ78633_SD ADS EVERYWHERE

SummaryEditCopy Master Control 2017062241090100 JGZ78633_HD ADS EVERYWHERE

SummaryEditCopy Master Control 2017062241090075 JWZ32568_SD ADS EVERYWHERE




Any help would be highly appreciated big time!!!

JJ

unread,
Jun 26, 2017, 2:37:21 PM6/26/17
to
Try using below batch (save it as "modfile.cmd").
Note: the space between "%%A" and "%%B" is a tab character. It's not
actually space characters. This is to preserve the original format. DO check
it after copy+pasting the code.

@echo off
setlocal enabledelayedexpansion
if not "%~2" == "" goto chksource
echo usage: modfile {source} {target}
goto :eof

:chksource
if exist %1 goto chkdest
echo Source file not found.
goto :eof

:chkdest
if not exist %2 goto start
echo Destination file is already exist and will be overwritten.
pause

:start
if exist %2 del %2
>%2 rem
set key=SD
for /f "tokens=1,2-4,5,*" %%A in (%1) do (
if not "%%A" == "" (
>>%2 echo %%A %%B %%C %%D %%E_!key! %%F
) else (
>>%2 echo.
)
if !key! == SD (set key=HD) else (set key=SD)
)

pro...@berkeley.edu

unread,
Jun 26, 2017, 5:26:38 PM6/26/17
to
@echo off
set X=SH
for /f "tokens=*" %%L in (test.txt) do call :MAIN "%%L"
goto :EOF

:MAIN
set "line=%~1"
set line=%line:~0,63%_%X:~0,1%D %line:~64%
echo/%line%
set X=%X:~1,1%%X:~0,1%
goto :EOF

Zaidy036

unread,
Jun 26, 2017, 10:51:20 PM6/26/17
to
I do not have time to make the batch but simpler and easier to read
might be:

:XXX
extract line 1 from OldFile.txt and SET _Line="line1"
ECHO %_Line: ADS=_DS ADS% >> NewFile.txt
extract line 1 and SET _Line="line1"
ECHO %_Line: ADS=_HD ADS% >> NewFile.txt
If OldFile is NOT empty GOTO XXX

petu...@googlemail.com

unread,
Jul 4, 2017, 11:19:28 AM7/4/17
to
Here's a version which determines the string to append the string to based upon its character set, i.e the only string containing exactly three alphabetic characters followed by five integers.

::----- START -----
@ECHO OFF
SET "srcFile=test.txt"
SET "dstFile=new.txt"
SET "#=\<[A-Z][A-Z][A-Z][0-9][0-9][0-9][0-9][0-9]\>"
SET "i=1"
(FOR /F "USEBACKQ DELIMS=" %%A IN ("%srcFile%") DO (SET/A i+=1
SET "$=%%A"
FOR %%B IN (%%A) DO ECHO %%B|FINDSTR "%#%">NUL&&SET "_=%%B"
SETLOCAL ENABLEDELAYEDEXPANSION
SET/A i %%=2|FIND "0">NUL&&(CALL ECHO=%%$:!_!=!_!_SD%%
)||CALL ECHO=%%$:!_!=!_!_HD%%
ENDLOCAL))>"%dstFile%"
::------ END ------

Each line has been prepended with two spaces; any line without a two space prefix has wrapped and requires fixing. Those spaces can optionally be removed.
0 new messages