Solved Copying Lines from a Text File Based On Input Value

June 13, 2012 at 07:16:19
Specs: Windows XP
Hi, I am totally a newbie to scripting in DOS CMD.

I have two text files, TxtData.txt and ParData.txt. TxtData.txt contains my raw data of multiple columns and rows. ParData.txt contains a one column list of data.

I would like to pick each value in ParData.txt and then look down the first column only in TxtData.txt and if I find a matching value on the first occurrence that I want that whole line copied to a new file named UpdData.txt. This script should loop until I have compared all the values listed in ParData.txt. My files are structured as follows:

ParData.txt
GL90P1
TA70P2
TA61P2
SO90P1
TA40P1
TA35P1
TA20P2
TA20P1
TA30P3
TA30P4

TxtData.txt
GL90P1 O5ER KAV 0290 OUTQK
TA70P2 O5PR LxE 0070 OMB01
TA61P2 O5PE LxE 0061 OMB01
SO90P1 O1ER LxE 0090 OMB01
TA40P1 O2ER LxE 0340 AR001
TA40P1 O3ER LxE 0340 AR001
TA35P1 OPER LxE 0135 AR001
TA35P2 O5ER LxE 0135 AR001
TA35P1 O6ER LxE 0135 AR001
TA20P2 O7ER LxE 0220 AR001
TA20P1 O9ER LxE 0220 AR001
TA30P3 O8ER LxE 0230 AR001
TA30P4 O6ER LxE 0230 AR001
TA30P3 O4ER LxE 0230 AR001

File UpdData.txt should look like this after the script has executed. All values in the first column should be unique. That is no duplicates.
GL90P1 O5ER KAV 0290 OUTQK
TA70P2 O5PR LxE 0070 OMB01
TA61P2 O5PE LxE 0061 OMB01
SO90P1 O1ER LxE 0090 OMB01
TA40P1 O2ER LxE 0340 AR001
TA35P1 OPER LxE 0135 AR001
TA35P2 O5ER LxE 0135 AR001
TA20P2 O7ER LxE 0220 AR001
TA20P1 O9ER LxE 0220 AR001
TA30P3 O8ER LxE 0230 AR001
TA30P4 O6ER LxE 0230 AR001

Thank you in advance for you help.


See More: Copying Lines from a Text File Based On Input Value

Report •


✔ Best Answer
June 15, 2012 at 21:00:09
oops!



#1
June 13, 2012 at 08:35:53
Untested
@echo off
for /f "tokens=1-4" %%a in (
ParData.txt
) do (
>>UpdData.txt findstr "%%a" TxtData.txt
))

Tony


Report •

#2
June 13, 2012 at 11:55:51
Thanks Tony. The script did not run so I removed the last close bracket in the last line and re-ran the script, however the next occurance of TA40P1, TA35P1 and TA30P3 in TxtData.txt got copied across to UptData.txt as well.

I need Uptdata.txt to contain only one instance of each variable that is listed in ParData.txt with its respective lines extracted from TxtData.txt.
As a quick check, the rows in UpData.txt should be equal to the rows in ParData.txt with the difference been the addition of the extracted lines from TxtData.txt.


Report •

#3
June 14, 2012 at 00:19:45
:: ===== script starts here ===============
::
:: yaus.bat Thu 14-06-2012 12:43:37.14
@echo off > newfile & setLocal enableDELAYedeXpansioN && @echo off > #

for /f "tokens=* delims= " %%a in (one) do (
find "%%a" < two >> #
)

for /f "tokens=1* delims= " %%a in (#) do (
find "%%a" < newfile > nul || >> newfile echo.%%a %%b
)

goto :eof

::====== script ends here =================


=====================================
Life is too important to be taken seriously.

M2


Report •

Related Solutions

#4
June 15, 2012 at 21:00:09
✔ Best Answer
oops!


Report •

#5
June 16, 2012 at 07:13:09
Thanks M2. The script creates temp file # then the newfile without any file extension and also all contents in TxData.txt. Still not archiving the result where:
1. The newfile is to be UpdData.txt.
2. UpdData.txt to contain each unique value as per the listing in ParData.txt. No duplicates.

Thanks also to nbrane (#4). Your script works perfectly with the data I have. However I still want to use ParData.txt as the file to provide the input variables to extract the data in TxtData.txt over to Upddata.txt.

I am thinking of combining M2 and #4's solutions, but how do I get to archieve that?


Report •


Ask Question