Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

String parsing

78 views
Skip to first unread message

Rick C.

unread,
Nov 12, 2005, 8:46:10 AM11/12/05
to
I have strings such as these:

'HELLO-1039-4019-3929'
'YES-1930-4910-4939'

with a variable number of characters before the first hyphen. I'd like
my command file to parse out and return to me the string 'HELLO' or
'YES': whatever exists before the first hyphen. Can someone point me in
the right direction of how to do this? Thanks much.

Timo Salmi

unread,
Nov 12, 2005, 9:14:29 AM11/12/05
to

@echo off & setlocal enableextensions
::
:: Make a test file
echo HELLO-1039-4019-3929 > mytest.txt
echo YES-1930-4910-4939 >> mytest.txt
::
::
for /f "tokens=1 delims=-" %%a in ('type mytest.txt') do (
echo %%a
)
::
:: Clean up
for %%f in (mytest.txt) do if exist %%f del %%f
endlocal & goto :EOF

Output:
C:\_D\TEST>cmdfaq
HELLO
YES

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

billious

unread,
Nov 12, 2005, 9:24:48 AM11/12/05
to

"Rick C." <blue--nos...@verizon.net> wrote in message
news:MPG.1ddfbc312...@news.individual.net...

for /f "delims=-" %%i in (fileofstrings.txt) do echo %%i

single-% if executed from the prompt, double if executed as a line within a
batch scriptfile.

Assuming, in the absence of information, that the strings are in a file and
the the single-quotes that you have used are quoted examples of the
structure of the lines in the file.

If the single-quotes are actually the first character in the line, then
include a single-quote somewhere between the equal-sign and the
closing-double-quote of the "DELIMS" clause.

HTH

...Bill


Rick C.

unread,
Nov 12, 2005, 9:53:44 AM11/12/05
to
Right, this looks great. Is there a way to do this without writing it
out to a file? My initial string is simply the value of a variable.

In article <dl4tc5$ge4$2...@haavi.uwasa.fi>, t...@uwasa.fi says...

billious

unread,
Nov 12, 2005, 10:32:30 AM11/12/05
to

"Rick C." <blue--nos...@verizon.net> wrote in message
news:MPG.1ddfcbffa...@news.individual.net...

> In article <dl4tc5$ge4$2...@haavi.uwasa.fi>, t...@uwasa.fi says...
>> Rick C. <blue--nos...@verizon.net> wrote:
>> > I have strings such as these:
>> > 'HELLO-1039-4019-3929'
>> > 'YES-1930-4910-4939'
>> > with a variable number of characters before the first hyphen. I'd like
>> > my command file to parse out and return to me the string 'HELLO' or
>> > 'YES': whatever exists before the first hyphen. Can someone point me
>> > in
>> > the right direction of how to do this? Thanks much.
>>
>> @echo off & setlocal enableextensions
>> ::
>> :: Make a test file
>> echo HELLO-1039-4019-3929 > mytest.txt
>> echo YES-1930-4910-4939 >> mytest.txt
>> ::
>> ::
>> for /f "tokens=1 delims=-" %%a in ('type mytest.txt') do (
>> echo %%a
>

> Right, this looks great. Is there a way to do this without writing it
> out to a file? My initial string is simply the value of a variable.
>

In your extensive testing, did you try

for /f "tokens=1 delims=-" %%a in ("%myvariablename%") do echo %%a

HTH

...Bill

Rick C.

unread,
Nov 12, 2005, 10:49:57 AM11/12/05
to
> In your extensive testing, did you try
>
> for /f "tokens=1 delims=-" %%a in ("%myvariablename%") do echo %%a
>
...("echo %myvariablename%")... works great. Thanks very much,
everyone; much appreciated.

Timo Salmi

unread,
Nov 12, 2005, 11:42:06 AM11/12/05
to
Rick C. <blue--nos...@verizon.net> wrote:
> In article <dl4tc5$ge4$2...@haavi.uwasa.fi>, t...@uwasa.fi says...
> > @echo off & setlocal enableextensions
> > echo HELLO-1039-4019-3929 > mytest.txt
> > echo YES-1930-4910-4939 >> mytest.txt
> > for /f "tokens=1 delims=-" %%a in ('type mytest.txt') do (
> > echo %%a

> Right, this looks great. Is there a way to do this without writing it

> out to a file? My initial string is simply the value of a variable.

Please don't take this the wrong way, but the general ideas should
be evident enough to continue from with one's own developments once
the essentials (in this case the for /f tokens delims) are given.
Let's bear in mind that this is not a helpdesk. Just a discussion
newsgroup. If you first are in a need of the very basics of script
writing you'll find it very useful to see the more than ample
material pointed to in

Windows Command Line Interface script programming links
http://www.uwasa.fi/~ts/http/http2.html#cmdscript

Meaning well.

0 new messages