Reputation: 285
I have a text file with following contents with some unwanted lines which I want to remove from the text file.
1233,pqry,01/11/2011
2365698,abcd,2011/01/12
,defghj,11/12/2013
,,,
,,,
,,,
In above example we can see there are multiple lines with comma separated. I want to remove those lines from my text file through batch file. Kindly help me out
Upvotes: 4
Views: 23968
Reputation: 70951
ren myFile.txt myFile.txt.old
findstr /v /b /c:",,," myFile.txt.old > myFile.txt
Rename file as .old. Find all the lines not containing ",,," at the begin of line in old file and save it to original file.
EDITED - to adapt to comments
If lines to be removed are those with only commas (i will include spaces) and no other value, then this should work
for %%t in (c:\path\*.txt) do (
ren "%%~ft" "%%~nxt.old"
findstr /v /r /c:"^[, ]*$" "%%~ft.old" > "%%~ft"
)
For each file in path, rename the file to *.old
, from this .old
file extract any lines not containing (/v
) the regular expression (/r
) defined by the pattern (/c
) : "from the start of line (^
), followed by any sequence of spaces and commas ([, ]*
) until reach the end of line ($
)". The extracted lines are send to the original filename.
Upvotes: 5