1.

Solve : want to remove particular string from a dat file through batch file.please help?

Answer»

I have to extract some set of record from the delivery FILE named delivery-2009.6.1-1.10.29172.dat based on the input from another file CALLED component.txt using a batch file having code as below.
=============================================
@echo off
REM *****************************************************************************
REM Batch file to read components from a file and extract matching lines from
REM archived delivery.dat files
REM
REM Input files: components.txt - list of components to be SEARCHED for
REM delivery*.dat - all archived delivery.dat files
REM Output file: deliver_results.dat - list of all delivery*.dat records with a
REM component in components.txt
REM ******************************************************************************

REM set value=

setlocal EnableDelayedExpansion

for /f "tokens=*" %%a in ('type components.txt 2^>NUL') do (
set value=%%a
echo value=!value!
findstr /s %%a delivery*.dat >> deliver_results.dat

if %errorlevel%==0 (
echo !value! Found - logged files into deliver_results.dat
) else (
echo No matches found
)
)
=============

I am not getting desired result in deliver_results.dat file.

In the output file I am seeing that always file name from which the record was extracted is showng at the beginning of the record which I don't want.Please suggest what can be done so that file name doesn't appear at the beginning.


Input component.txt file looks like

3103458901
0031029107
and so on

Input delivery file named delivery-2009.6.1-1.10.29172.dat looks like

"100026390","06 ","KI3","3103458901/KI3 ","RECEIPT"
"100025696","07 ","LE3","0031004367/LE3 ","RECEIPT"

Output deliver_result.dat file looking like

delivery-2009.6.1-1.10.29172.dat:"100026390","06 ","KI3","3103458901/KI3 ","RECEIPT"

I don't want this file name "delivery-2009.6.1-1.10.29172.dat:" to be appearing in deliver_results.dat.
output should be like the file delivery-2009.6.1-1.10.29172.dat ..

Please modify the code so that I can get desired result.That will be great help.
this is not batch. if you have Python on windows
Code: [Select]import os,glob
component="component.txt"
comp=open(component).read().split("\n")
path=os.path.join("c:\\","test")
os.chdir(path)
for files in glob.glob("delivery*"):
for line in open(files):
line=line.strip()
oline=line.split(",") #split the line on comma
#try to get 3103458901 from "3103458901/KI3 "
number = oline[3].split("/")[0].replace('"',"")
if number in comp:
print ','.join(oline)
save the above myscript.py and on command line:
Code: [Select]C:\test>python test.py > delivery_result.dat

as for the batch part where you don't want the filename, set delims to ":" and set also your tokens (i believe you now how to use it already)...then use the for loop to get rid of it.type delivery*.dat|findstr/sl "%%a">>deliver_results.dat
Quote from: Reno on June 05, 2009, 12:48:53 AM

type delivery*.dat|findstr/sl "%%a">>deliver_results.dat

findstr can find MULTIPLE files, so type is not needed
Code: [Select]findstr ..... delivery*dat >> results.dat
Quote from: gh0std0g74 on June 05, 2009, 01:45:38 AM
findstr can find multiple files, so type is not needed
Code: [Select]findstr ..... delivery*dat >> results.dat

dude, it will display the filename in front of the found line when use in conjunction with file mask delievery*.dat. therefore type is needed to pipe into findstr.

after testing, i revised a bit on the code, it should be:
Code: [Select]type delivery*.dat 2>nul|findstr/sl "%%a">>deliver_results.datCode: [Select]FOR /F "tokens=2* delims=:" %%k in ('findstr /i %%a *.txt') do ( echo %%k >> newfile )


Discussion

No Comment Found