Operating System - OpenVMS
1748126 Members
3255 Online
108758 Solutions
New Discussion юеВ

Re: Extracting a specific string

 
Marcos Llano
Occasional Advisor

Extracting a specific string

Hi,

I'm looking to extract and specific string from a file. Basically, first I execute a ping to a host (before will be 20) that the result go to a file.
Then, a job try to search what % of packets have lost. Well, with the lexical F$EXTRACT and F$LOCATE I can only print "0% packet loss" but putting a lot of condicionals, and I don't like much this metod. How could I extract only the 0% (or 100%)? With this value

Thanks and regards
9 REPLIES 9
abrsvc
Respected Contributor

Re: Extracting a specific string

If I understand your requirements correctly, you wish to extract the percentage loss only. If that is the case, use the F$locate looking for "received," and then for the string "% packet" and extract the characters between those positions. Use the f$integer to change to a value.

Dan
Joseph Huber_1
Honored Contributor

Re: Extracting a specific string

If the variable line contains
"4 packets transmitted, 4 packets received, 0% packet loss"

Then parse it like:
line=f$edit(line,"TRIM,COMPRESS")
rest=f$edit(f$element(2,",",line),"TRIM")
perc=f$element(0," ",rest)
show sym perc

I broke it into several stements for clarity, You can put everything into a single f$element(...) statement.
http://www.mpp.mpg.de/~huber
Hoff
Honored Contributor

Re: Extracting a specific string

There are source code versions of ping around, and you can tweak one to meet your needs.

http://www.ping127001.com/pingpage.htm

http://www.cs.utah.edu/~swalton/listings/sockets/programs/part4/chap18/ping.c

There's a callable ping tool on Freeware V8.0.

There are also free and commercial services around such as the pingdom service, and any number of monitoring tools such as nagios nrpe that can be used to monitor uptime.
Marcos Llano
Occasional Advisor

Re: Extracting a specific string

Hi,

A lot of thanks Joseph and abrsvc. I have exactly that i wanted

$ OPEN IN OPER00DIR:PING.DAT
$ LOOP:
$ READ/END_OF_FILE=ENDIT IN NAME
$ HOST = F$EXTRACT(F$LOCATE("4 packets",NAME),100 ,NAME)
$ HOST=f$edit(HOST,"TRIM,COMPRESS")
$ rest=f$edit(f$element(2,",",HOST),"TRIM")
$ perc=f$element(0," ",rest)
$ IF HOST .EQS. ""
$ THEN
$ GOTO LOOP
$ ELSE
$ SHOW SYMBOL PERC
$ ENDIF
$ ENDIT:
$ CLOSE IN

Now, with this value can use it to know wicht devices are working, etc and a new syntaix for future

Thanks againt for your help
Marcos Llano
Occasional Advisor

Re: Extracting a specific string

Done
Hein van den Heuvel
Honored Contributor

Re: Extracting a specific string

Marcos,

You may want to take a step back and look at the larger problem you are trying to solve.
What are you going to do next with that PERC symbol value?

I can parse text files with DCL with the best of them, and even enjoy doing that (yeah, twisted person), but sometimes there are better tools.

For just playing around with strings I find PERL and AWK very convenient.

Here, for example to just get that specific string:

perl -ne "print $1 if /(\d+)% packet loss/" tmp.tmp

Want to add the host name to that?

perl -ne "$host=$1 if /^----(\S+)\s+PING/; print qq($host $1) if /(\d+)% packet loss/" tmp.tmp

Enjoy!
Hein

Marcos Llano
Occasional Advisor

Re: Extracting a specific string

Hi,

The objetive of this job is know which terminal are in use after working hour. So I need a value to know if the terminal is switched-on. My first idea was make a ping. If don't response, then the Thinclient are swiched-off.
Of course, I can use a Nagios system to monitor if a terminals are swhitched-on or not. But I think that this way is more interesting to learn and gain experience (I'm new in this environment)

Thanks and regards
Hein van den Heuvel
Honored Contributor

Re: Extracting a specific string

If you are new to this, and want to make it more interesting then I urge you to pick up Perl and for this specific exercise check out its 'ping' extension. This will allow you to build a nicely integrated solution which can run unaltered on OpenVMS, Linux or Windoze as you see fit.

fwiw,
Hein.
Hoff
Honored Contributor

Re: Extracting a specific string

perl has a ping module available, and python and other scripting languages make this task trivial.

I'd even use C here (with any of the available ping libraries) long before I'd try using DCL, too. DCL is an increasingly poor choice.

That written, it would not surprise me to learn that this problem has already been solved. Dozens of times. Why even write code if you don't need to?