Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
renzlo
- Posts: 116
- Joined: 03 May 2011 19:06
#1
Post
by renzlo » 06 Oct 2011 23:26
Hi All,
I want to split below contents of source.txt,
Code: Select all
0.99Flakes in oil
2.99Safe Guard
0.0035Fleeming
12.999Tissue Holder
123.25-Oasis
1245.88 Amnna
The numbers are price and the alpha are product name:
I want the output to be like this(tab delimited):
Code: Select all
Price Product Name
0.99 Flakes in oil
2.99 Safe Guard
0.0035 Fleeming
12.999 Tissue Holder
123.25 Oasis
1245.88 Amnna
I tried everything I know but no luck for me, is there a way?
-
colargol
- Posts: 49
- Joined: 28 Sep 2011 13:23
- Location: france
#2
Post
by colargol » 07 Oct 2011 05:13
Hi
I think this should work. I don't know if there is a better solution...
Code: Select all
setlocal ENABLEDELAYEDEXPANSION
for /f "usebackq tokens=*" %%a in (`type "file.txt"`) do (
set line=%%a
for /f "tokens=* delims=1234567890." %%b in ("!line!") do (set product=%%b)
call set "price=%%line:!product!=%% "
for /f "tokens=* delims= " %%b in ("!product!") do (set product=%%b)
echo !price:~0,16!!product!
)
assuming price won't exceed 16 chars and products won't have special chars like ! ^ =
-
renzlo
- Posts: 116
- Joined: 03 May 2011 19:06
#3
Post
by renzlo » 07 Oct 2011 06:21
thanks for the reply dude. I'll try this one. If you don't mind, kindly explain this line:
Code: Select all
call set "price=%%line:!product!=%%
-
colargol
- Posts: 49
- Joined: 28 Sep 2011 13:23
- Location: france
#4
Post
by colargol » 07 Oct 2011 12:49
sorry for my bad english, it's not always easy to explain.
This is just a string substitution (
see here)
In each line, the product is replaced with nothing, so we get the price
I used call because it allows to use a variable in the substitution.
Not sure I'm clear.