1.

Solve : Search for String in file and remove it?

Answer»

Hi
I have CSV data like

ERM,VU35201, COMP Ltd,VU35201,INS071,INS071,INS071,-157,USD,,,,,-157.00,-157.00,,,O,Open,18122007,19011900,,18122007,,,,,,,,,,19011900
ERM,WO01063,COMP Ltd,WO01063,SON010,SON010,SON010,-13,GBP,,,,,-13.00,-13.00,,,O,Open,01062006,19011900,,01062006,,,,,,,,,,19011900
ERM,WO08022,COMP Ltd,WO08022,SAA002,SAA002,SAA002,-44,GBP,,,,,-44.00,-44.00,,,O,Open,02082006,19011900,,02082006,,,,,,,,,,19011900
ERM,XXXXX,COMP Italy,XXXXX,SOL003,SOL003,SOL003,-870,EUR,,,,,-870.00,-870.00,,,O,Open,19032009,19011900,,19032009,,,,,,,,,,19011900
ERM,XXXXX,COMP Ltd,XXXXX,ASS001,ASS001,ASS001,-1522,GBP,,,,,-1522.00,-1522.00,,,O,Open,12032009,19011900,,12032009,,,,,,,,,,19011900
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,

The last commas with out data are causing problem for my load. I wanted to write Batch cmd to remove those .I tried with some code i FOUND but it gives error. I cant use any EXTERNAL pkg only batch.

This is something that i found on net but giving error
@echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
set file="C:\myfile.csv"
if "%*"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %file%|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:%~1=%~2%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
) ELSE echo.
)


Regards
Amit

you do not expect to plug something you found on the net and expect it to work without understanding and modifying it, do you?. here's a vbscript one
Code: [Select]Set objFS = CreateObject("Scripting.FileSystemObject")
strFile= "C:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile,1)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If Left(strLine,2) = ",," Then
WScript.Echo strLine
End If
Loop
save as myscript.vbs and on command line
Code: [Select]C:\test>cscript /nologo test.vbs
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,
the output is not what you want. i leave it to you to change that. just have to invert the condition in the "if" statementthat's look like my coding style.
except i dont write in uppercase, i dont use REM to comment, i use ::.

anyway josh, here is the one liner solution:
findstr/v /b ",," data.csv

lol, i need to think faster :/ ghostdog beats me by 3minuteQuote from: Reno on April 03, 2009, 04:22:33 AM

i dont use REM to comment, i use ::.

Using a broken label as comment starter in a loop or other parenthetical structure will break it.

however...

Code: [Select]@echo off

% This is a comment %

echo hello world

for %%A in (a b c d e) do (

% This is a comment %
echo %%A

)





Quote from: Dias de verano on April 03, 2009, 04:50:07 AM
Using a broken label as comment starter in a loop or other parenthetical structure will break it.
here is a good explanation on double colon :: or using REM as comment:
http://www.robvanderwoude.com/comments.php

it seems to be common practice in batch to use ::., and i personally prefer :: double colon, because it makes my code more readable. ok, not really, in fact, i rarely put comment on my batch code.

now, here is another case, which one would be executed faster:
1. for /f "tokens=*" - default delims is {TAB}{SPACE}
2. for /f "delims=" - default token is 1
since microsoft don't publish his source code, it looks like no.2 will be faster by a split nano-second because it don't do parsing operation. but my preference is no.1, because it seems code is more readable using no.1
unless microsoft pseudocode is:
Code: [Select]s=readline
if tokens=* or delims is empty then
//do nothing
else
//do parsing on string s
end if

well, in batch, there is no need to follow the rule of correct syntax.
here is an example to count number of words, with limitation
Code: [Select]set s=sample test string
set/a n=1+.%s: =&set/a n+=1+.%%
echo words=%n%Quote from: Reno on April 04, 2009, 07:38:59 AM
here is a good explanation on double colon :: or using REM as comment:
http://www.robvanderwoude.com/comments.php

Yes, I am very familiar both with Rob's pages and with the arguments for and against using :: as a comment marker.

Quote
it seems to be common practice in batch to use ::

A bad practice in my opinion, (but only my opinion!)

Quote
., and i personally prefer :: double colon,

see above.

Quote
because it makes my code more readable.

That is, again, a matter of opinion. batch code has different readability requirements compared to (e.g.) poetry.

Quote
ok, not really, in fact, i rarely put comment on my batch code.

Like many sloppy programmers?

Quote
now, here is another case, which one would be executed faster:
1. for /f "tokens=*" - default delims is {TAB}{SPACE}
2. for /f "delims=" - default token is 1

Doesn't matter. Nobody codes in batch for speed.

Quote
code is more readable using no.1

Code is more readable (to the coder) if it contains that coder's preferences, then (a circular argument)?

Quote
well, in batch, there is no need to follow the rule of correct syntax.

So you say

Quote
here is an example to count number of words, with limitation
Code: [Select]set s=sample test string
set/a n=1+.%s: =&set/a n+=1+.%%
echo words=%n%

That is a hack!

When writing your own batch code, comment style and obeying rules of syntax are of course optional. However this is a forum where we give guidance to new and/or confused users, and in that situation, in my opinion, clarity and ease of understanding are more important than clever coding tricks.

for comments, I prefer /*...*/ blocks. but every time I try and use it, my batch breaks! so I change it back to

Code: [Select]REM now, use xcopy to copy the favourite goat pictures to a backup disk.

xcopy "..\Pics\Stay out\Hey, I said stay out\OK, seriously, how many times do I have to tell you\ok fine,
(wrapped) COME on in\well there goes my reverse psycology idea\more goat pics\Bessie" I:\BACKUPS\BESSIE /S

I just counted my BCFile project, and it contains 9,416 (72%) Code lines, and 3,518 (27%) comment lines.

and as for esoterics and readability, I'm probably at my worst in this section with string manip. I've become so practices with the VB string manipulation functions, I simply nest a bunch of them on one line,- a sample dug out from deep within my BASeParser evaluator LIBRARY:

Code: [Select] SplMake(CurrArgument) = Mid$(FromString, ArgStart, (CurrPos - ArgStart) + 1)
If right$(SplMake(CurrArgument), 1) = ARGUMENTSEP Then
SplMake(CurrArgument) = Mid$(SplMake(CurrArgument), 1, Len(SplMake(CurrArgument)) - 1)
End If

Quote from: BC_Programmer on April 04, 2009, 10:52:46 AM
I've become so practices with the VB string manipulation functions, I simply nest a bunch of them on one line

I do this a lot in VB6, VBScript, QB etc.
QB is a pain. I keep trying to use TRIM$... but there isn't one. so I end up doing LTRIM$(RTRIM$()) which is slightly longer.

I just can't stand that technically VB is working with an immutable string and always creates a completely new string...I don't actually use QB any more. That Ltrim(Rtrim(string)) thing was the first thing that popped into my head when I read your remark about nesting string functions all on one line.

you use Freebasic. I would too, but I managed to hack pretty good console support into Visual Basic, which would likely have been my only reason to use FreeBASIC.Code: [Select]@echo off
set '=REM
%'% this is a comment
echo hello world
Quote from: Dias de verano on April 04, 2009, 01:37:00 PM
Code: [Select]@echo off
set '=REM
%'% this is a comment
echo hello world
hahaha, nice one, almost got me.
C:\>set '
'=REM

Quote from: Dias de verano on April 04, 2009, 09:17:07 AM
That is, again, a matter of opinion. batch code has different readability requirements compared to (e.g.) poetry.

Like many sloppy programmers?
yes i am one of them , i dont have the time to write any comment about the code, just too busy thinking more trick to cramp multiple lines of codes into one line. and i always like one liner code which do the same job as the for example 10 lines of if else if else for loop.
only in batch though, not so easy to do with other programming language, such as vb, vbs, etc.

Quote from: BC_Programmer on April 04, 2009, 11:37:58 AM
I would too, but I managed to hack pretty good console support into Visual Basic, which would likely have been my only reason to use FreeBASIC.
BC, how do you implement console support into VB aps?
do you use reference to scrrun.dll Scripting.FileSystemObject, or using GetStdHandle API then later use link.exe or something else?Quote from: Reno on April 05, 2009, 01:13:38 AM
hahaha, nice one, almost got me.
C:\>set '
'=REM

Huh?

Code: [Select]S:\Test\Batch>type remtest.bat
@echo off
set '=REM
%'% this is a comment
echo hello world

S:\Test\Batch>remtest.bat
hello worldQuote from: Dias de verano on April 05, 2009, 02:38:24 AM
Huh?

Code: [Select]S:\Test\Batch>type remtest.bat
@echo off
set '=REM
%'% this is a comment
echo hello world

S:\Test\Batch>remtest.bat
hello world
at first, i though you use vb commenting style, wait, how that's going to work? then i realize you just create a variable ' with value REM. lol.

Quote from: Reno on April 05, 2009, 03:03:23 AM
i realize you just create a variable ' with value REM. lol.

Good old cmd.exe runtime variable expansion

Code: [Select] @echo off
set gosub=call
set return=goto :eof

echo in main
%gosub% :sub1
goto :end

:sub1

echo in subroutine
%return%

:end
Quote from: Reno on April 05, 2009, 01:13:38 AM
only in batch though, not so easy to do with other programming language, such as vb, vbs, etc.
Don't be silly. Take this, for instance:
Code: [Select]Public Function InStrCount01( _
String1 As String, _
String2 As String, _
Optional ByVal Start As Long = 1, _
Optional Compare As VbCompareMethod = vbBinaryCompare) As Long


Dim lenFind As Long

lenFind = Len(String2)

If lenFind Then
' silently correct illegal Start value
If Start < 1 Then
Start = 1
End If
Do
Start = InStr(Start, String1, String2, Compare)
If Start Then
InStrCount01 = InStrCount01 + 1
Start = Start + lenFind
Else
Exit Function
End If
Loop
End If

End Function

I say BAH to that procedure, This one is much better:

Code: [Select]Public Function InstrCount(ByVal InString As String, ByVal StrFind As String, Optional ByVal Start As Long = 1, Optional ByVal compare As VbCompareMethod = vbBinaryCompare) As Long
If StrFind = "" Then
InstrCount = 0
Else

InstrCount = (Len(InString) - Len(Replace$(InString, StrFind, "", Start, , compare))) / Len(StrFind)
End If

End Function

Which ties in with the whole bunch of string functions/operations one line thing.

Quote
BC, how do you implement console support into VB aps?
do you use reference to scrrun.dll Scripting.FileSystemObject, or using GetStdHandle API then later use link.exe or something else?

It's a secret ... sort of.

But I'll share. Essentially the second method, using WriteFile,ReadFile on the Handles returned by GetStdHandle. It's wrapped in a neat module that I can drop into each Console program.

I then run this VBScript, which runs my LINK.EXE (as you've said) to switch the executables subsystem:



Code: [Select]
Option Explicit

Dim strLINK, strEXE, WSHShell

' Be sure to set up strLINK to match your VB6 INSTALLATION.
strLINK = """D:\Programs\Microsoft Visual Studio\VB98\LINK.EXE"""

strEXE = """" & WScript.Arguments(0) & """"

Set WSHShell = CreateObject("WScript.Shell")

WSHShell.Run strLINK & " /EDIT /SUBSYSTEM:CONSOLE " & strEXE

Set WSHShell = Nothing
WScript.Echo "Complete!"


I normally only use the scrrun FSO for VBScript's designed for others- I've been trying to give my BCFile library some exercise. Main benefit being that it can show the explorer menu for any file/folder- as well as including a FileSearch and FileSearchEx classes.


Discussion

No Comment Found