Link to home
Start Free TrialLog in
Avatar of Govinda2020
Govinda2020Flag for Ireland

asked on

Delete rows using batch file

I am trying to delete all lines starting with either GBP or USD , and also delete first line using batch script.I am using the below code which is not working as expected.


@echo off > data1.csv & setLocal enableDELAYedexpansion
for /f "tokens=* delims= " %%a in (data.csv) do (
set s=%%a
set s=!s:~3!
>> data1.csv echo.!s!
)
Sample.txt
SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry Steve, you can't do that - it's TOO simple !! LOL

While you're at it, you may like to remove the spaces around the pipe and redirection symbols to conserve a few more characters. Oh, but don't forget the '/i' switch in FINDSTR. Like this:

more +1 data.csv|findstr /b /i /v "GBP USD">data1.csv
Oh come on Paul... cutting out spaces!   The /i is only relevant if he wants it case-insensitive which may well be an issue I suppose.

Surprised you didn't get that one :-)

Steve
Avatar of oBdA
oBdA

Sorry, but it's not that easy with findstr; that version will delete all lines containing "GBP" or "USD" anywhere in the line, not "[...] all lines starting with either GBP or USD".
So if there happens to be an entry "NZDUSD", it will be removed as well, and so will any currency exchange that contains "USD" or "GBP" anywhere in the string (like "XUSDXX").
To fix that, you'd need to work with regular expressions, and Microsoft has implemented their very own, stripped down, interpretation of REs in findstr ("|" as alternation isn't supported, for example).
And then "more" isn't actually needed to drop the header line:
findstr /V /I /R "^EUR ^GBP ^ac:string,ac:double$" "data.csv" >"data1.csv"

Open in new window

Steve

I modelled my first reply closely to Govinda2020's own code... as it wasn't a million miles off track (just a few thousand!).

Looking at the attached Sample.txt file (which I missed first time around) we can safely remove the '/i' switch...

more +1 data.csv|findstr /b /v "GBP USD">data1.csv
oBdA

more +1 data.csv|findstr /b /v "GBP USD">data1.csv

Works just great for me because of the '/b' switch in FINDSTR.
I can get this down to just 33 characters if the filenames are just 'A' and 'B' and the two spaces infront of '/b' and '/v' are removed:

more +1 A|findstr/b/v "GBP USD">B

BTW, you'll notice I did not enclose the filenames in double-quotes as there are no spaces in them !! LOL
That's true, missed that. But the "more" is still not required ...
findstr /V /I /B "EUR GBP ac:string,ac:double" "data.csv" >"data1.csv"

Open in new window

Which would allow for 4 bytes less, while we're at it:
findstr /b /v "GBP USD ac:" A>B

Open in new window

oBdA

I can improve upon that by removing the space infront of the '/b' and looking at the sample data 'a' would suffice instead of 'ac:'.

I've now got this down to a meagre 28 characters which beats your 31! LOL

findstr/b /v "GBP USD a" A>B
Finally, looking at the three-letter ISO Currency Codes, it's safe to use just 'GB' and 'US' instead of 'GBP' and 'USD'!

Just 26 characters and surely that must be it!

findstr/b /v "GB US a" A>B

LOL !!!
All potentially valid and good fun to save a byte or or two... maybe valid in a 16k 1980's computer or embedded system but in a PC batch file in 2012?!

We can speculate that the first line is always as shown but why not just make sure in case the format changes and exclude the first line with a more +1.

Anyway upto the asker if we haven't lost him.  Aside from the 'extras' the first three posts should be OK, personally I like my suggestion!
Avatar of Govinda2020

ASKER

It's great to see experts express their expertise, I used the below , as I have to remove first three characters after removing rows with "GBP" or "USD". Any suggestions for improvment.

more +1 data.csv|findstr /b /i /v "GBP USD">data1.csv

@echo off > data2.csv & setLocal enableDELAYedexpansion
for /f "tokens=* delims= " %%a in (data1.csv) do (
set s=%%a
set s=!s:~3!
>> data2.csv echo.!s!
)

del "data1.csv"

exit
Well you could do it all in one for loop but if you want to keep the findstr / more still you can change your for command to use the output without the intermediate data1.csv:

@echo off
setLocal enableDELAYedexpansion

(for /f "tokens=* delims= " %%a in ('more +1 data.csv^|findstr /b /i /v "GBP USD"') do (
set s=%%a
echo !s:~3!)
)> data2.csv

Steve
That works perfect @ dragon-it , one more last question surely. I am trying to delete currency INR after above code(it's not in the sample file). Again I have used the 2 files, any suggestion.

@echo off
setLocal enableDELAYedexpansion

(for /f "tokens=* delims= " %%a in ('more +1 data.csv^|findstr /b /i /v "GBP USD"') do (
set s=%%a
echo !s:~3!)
)>data1.csv

findstr /b /i /v "INR" data1.csv > data2.csv
You could do that - what you have there should work, or can just add an extra entry on the existing first findstr line "GBP USD INR".  You can add any number of entries here.  We can get more complicated but the basic check /B is beginning of line and it will exclude (/v) any entries space seperated in the " "

Steve
That's fine , Is it possible to give 500 points to everyone who answered?
No, sorry you can split evenly, or spread as you wish.  While we are here for the points it;s not the end of the world.  Just select one(s) that have been useful to you or you have used in the end

Steve
Hey Steve

...personally I like my suggestion!
Funnily enough, personally I like MY suggestion too!!

(Note the additional exclamation mark adding greater emphasis to my statement. LOL).

I'm good with a split... Was fun!
Govinda2020

Can I have the first 500 points please?...

LOL !!
Very quick and helpful