Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capturing data strings

Status
Not open for further replies.

milo63

Technical User
Jul 3, 2002
17
US
I am new to Aspect and I hope someone can get me started in the right direction. I am trying to capture several data strings and assign them to string variables. I will be getting the data from a Sun (Unix) server and need to know the best way to collect the data. Here is an example of what I want to do:
Open the file "/vol/pat/peers.cfg"
Search for all lines with "node ="
this should output something like "node = host1 " and "node = host2 " (without quotes)
Search for all lines with "port ="
this should output something like "port = 104 " and "port = 4004 "
I need to strip trailing spaces and assign them to string variables. The string lengths will vary also.
My 4 variable stings values should be host1 host2 104 and 4004
Print out string variables values.

Should I use unix grep and fgrep commands to filter down to the data string I am looking for and then do a screen capture or go right to the file and do a string capture? Any help is appreciated.
 
Depending on how large the file is, I would probably recommend the grep of the file as you mentioned, then read that data from the screen. If the same number of lines will be output each time the script is run, then you could use the necessary number of rget commands (don't forget to factor in any blank lines that may be output before the data is shown) to read in each line of text. You may also need to change the rget character from a CR (decimal 13) to a LF (decimal 10), at least I did on this Sun machine. After you have the string read in, you can then use the different ASPECT string commands to strip out the text you are not interested in or only extract the text you want (how to do this depends on just what the strings look like). Here's some code that will strip all trailing spaces from a string. It's a modification of a function I posted a few days ago to remove leading spaces. I just used strrev to reverse the string, then used the same function code to remove the now-leading spaces, before reversing the string when I'm done.

proc main
string foo = "test123 "
StripSpaces(&foo)
endproc

func StripSpaces : string
param string targetline
integer str_length

strlen targetline str_length
strrev targetline
while str_length > 0
if rstrcmp targetline " " 1
strdelete targetline 0 1
else
exitwhile
endif
endwhile
strrev targetline
return targetline
endfunc

 
This is the command I am running and the output that follows:

PEGASYS@adac_x:#[6] cat /vol/patients_db/dicom/dicompeers.cfg |egrep '%|node|port|calling|called' |fgrep -v #
%ALI_PACS
node = nserv0
port = 4000
callingAEtitle = ALI_STORE_SCP
calledAEtitle = DICOM_SCP
%PACS_CUBE
node = pacscube
port = 4006
callingAEtitle = ADAC_SCU
calledAEtitle = RIMAGE1
%Mitra_RIS
node = broker
port = 3320
callingAEtitle = ADAC_SCU
calledAEtitle = BROKER
%ALI_Broker
node = alibroker
port = 3320
callingAEtitle = broker
calledAEtitle = DICOM_SCP

each entry (the % line plus the next 4 lines) would need to be captured and stripped down to the datastring. In this example, the last entry (ALI_Broker) would have 5 strings defined - ALI_Broker, alibroker, 3320, broker,DICOM_SCP.
Would need to strip "%" from the first line, "node = " from the second line etc... and the strip trailing spaces. I will try your script once I get to that point. Here there are only 4 entries but other systems might have 6 or 7 entries and will scroll off the screen so I would not be able to capture screen position (termgets). Is there an easier way? -Mike
 
In the case of variable number of lines being returned, you could use the when target command to fire on the % character, and then in the procedure that is called use five rget commands (one for each line that should be displayed), then parse those five string variables for the information you require. Once that procedure has completed, it will return to the main script, detect that another % character has been received, the procedure to read the data will be called again, etc. I haven't had time to test this theory, but it should work I believe as long as the % sign only occurs in the first line of each set of data.

As for parsing out the results of a line of the format xxx = yyy, you could use the strextract command as such:

strextract NewString OldString "=" 1

where OldString is the string you read using the rget command and NewString is the parsed result. The "=" argument tells strextract to use the equals sign as the common separator of the string (the equal sign must be in double quotes or the compiler gripes) and the 1 indicates to return the second value of the string. You will need to run the string through the function I sent you, once with the string reversed and once with it as-is since there will be one leading space and any number of trailing spaces in the string. Actually now that I think of it, you could use this:

strreplace NewString " " ""

to remove all spaces from the extracted string. This should work as long as you'll never have a line that looks like "node = alibroker test" (i.e. no spaces in the data).

 
Mike,

This should get you started. Letme know if any further Assistance is Needed.

Hank

proc Main

string sLine
string hTok1
string nTok1,nTok2,nTok3
string pTok1,pTok2,pTok3
string gTok1,gTok2,gTok3
string dTok1,dTok2,dTok3
string OutPut

fopen 1 "C:\Files\Nodes.txt" READ TEXT
while not feof 1
fgets 1 sLine
if strfind sLine "%"
strtok hTok1 sLine " " 1
strdelete hTok1 0 1
endif
if strfind sLine "node "
strtok nTok1 sLine " " 1
strtok nTok2 sLine " " 1
strtok nTok3 sLine " " 1
endif
if strfind sLine "port "
strtok pTok1 sLine " " 1
strtok pTok2 sLine " " 1
strtok pTok3 sLine " " 1
endif
if strfind sLine "callingAE"
strtok gTok1 sLine " " 1
strtok gTok2 sLine " " 1
strtok gTok3 sLine " " 1
endif
if strfind sLine "calledAE"
strtok dTok1 sLine " " 1
strtok dTok2 sLine " " 1
strtok dTok3 sLine " " 1
strfmt OutPut "%s,%s,%s,%s,%s" hTok1 nTok3 pTok3 gTok3 dTok3
Termwrites OutPut
Termwrites "`n`r"
endif
endwhile
fclose 1
endproc
 
I tried this but I am getting an "invalid identifier" error when the script runs. Here is what I have:

proc main
string sLine
string hTok1
string nTok1,nTok2,nTok3
string pTok1,pTok2,pTok3
string gTok1,gTok2,gTok3
string dTok1,dTok2,dTok3
string Output

transmit "^M"
waitfor "]"
clear
transmit "cat /vol/patients_db/dicom/dicompeers.cfg |egrep '%|node|port|called|calling' |fgrep -v #^M"
waitfor "]"

fopen 1 "C:\Files\Nodes.txt" READ TEXT
while not feof 1
fgets 1 sLine
if strfind sLine "%"
strtok hTok1 sLine " " 1
strdelete hTok1 0 1
endif
if strfind sLine "node ="
strtok nTok1 sLine " " 1
strtok nTok2 sLine " " 1
strtok nTok3 sLine " " 1
endif
if strfind sLine "port ="
strtok pTok1 sLine " " 1
strtok pTok2 sLine " " 1
strtok PTok3 sLine " " 1
endif
if strfind sLine "callingAE ="
strtok gTok1 sLine " " 1
strtok gTok2 sLine " " 1
strtok gTok3 sLine " " 1
endif
if strfind sLine "calledAE ="
strtok dTok1 sLine " " 1
strtok dTok2 sLine " " 1
strtok dTok3 sLine " " 1
strfmt Output "%s,%s,%s,%s,%s" hTok1 nTok3 pTok3 gTok3 dTok3
Termwrites Output
Termwrites "`n`r"
endif
endwhile
fclose 1
endproc

Do you see anything wrong here? -Mike

 
Mike,

You'll need to setup a Capture Session and the Transmits, right after the Main procedure.

Something Like This:

proc main
string sLine
string hTok1
string nTok1,nTok2,nTok3
string pTok1,pTok2,pTok3
string gTok1,gTok2,gTok3
string dTok1,dTok2,dTok3
string Output

clear
string szName = "Node_data.cap" ; Create Capture File Name
clear ; Clear the Procomm Screen
set capture overwrite on ; Overwrite Existing INQ.cap File
set capture query off ; Do Not Request Query of File Name
set capture file szName ; Set the Capture File Name
Capture on ; Turn On the Capture File (Open)

transmit "^M"
waitfor "]"
transmit "cat /vol/patients_db/dicom/dicompeers.cfg |egrep
Capture off

fopen 1 "C:\Files\Node_data.cap" READ TEXT
while not feof 1
fgets 1 sLine
*
*
*

ALSO, you will need to check you Setup for the Path that the Capture Files are Send to and Modify the FOPEN Path. Also are you sending This with a Direct Connect ??

Let Me Know

Hank
 
I tried this. I'm still getting "error 5: invalid identifier". I verified my path (C:\files\Node_data.cap). I am using a modem connection. I have a separate script to set up my terminal. I checked my Node_data.cap file and it is empty. Other ideas?
 
By the way, I am assuming the Node_data.cap file is on my pc, not the remote host. If Node_data.cap is on the remote host would you still use fopen, since it is a Sun (unix) box?
 
Mike,

The Error 5 Invalid File Identifer is a PC Path Problem. In my Example "C:\Files" was Created by me for Test Purposes. On Your PC you'll have to Check the Setup in Procomm for the Capture Path. I assume that after the Connection is Established and you issue the Unix CAT Command the Data comes across your Terminal Screen..

One way to Test the Setup Path is to look in Procomm: [ Options - Data Options - Paths - Capture Path ]. Use this Path in the Procomm FOPEN Statement.

Let me Know

Hank
 
That worked. The capture file looks good. How can see the values of the string variables?
 
Mike,

Glad to Help. If you want to Display the Various Stings, you can use the Tremwrites Statement [ Termwrite hTok1 ]. The Termwrites "`n`r" is a LF/CR.

Also Note that you can use the FPUTS Statement if you want to send the Results to a File on your PC. For this You'll need to use an Additional FOPEN Statement as shown below.

Fopen 2 "C:\Files\Node_Data.cap" Create Text
While........
Fgets 1 sLine
Fputs 2 Output % Replaces the Termwrites Output
*
*
*
Fclose 1
Fclose 2

Hank
 
Getting closer... However the data strings are being repeated 5 times for each string. Here is an example:

[Script]:
proc main
string sLine
string hTok1
string nTok1,nTok2,nTok3
string szName = "Node_data.cap"

clear
set capture overwrite on
set capture query off
set capture file szName

clear
Capture on
transmit "cat /vol/patients_db/dicom/dicompeers.cfg |egrep '%|node|port|called|calling' |fgrep -v #^M"
waitfor "]"
Capture off

fopen 1 "C:\program files\procomm plus\capture\Node_data.cap" READ TEXT
while not feof 1
fgets 1 sLine
if strfind sLine "%"
strtok hTok1 sLine " " 1
strdelete hTok1 0 1
endif
transmit "^M"
Termwrites hTok1

if strfind sLine "node = "
strtok nTok1 sLine "node =" 1
strtok nTok2 sLine "node =" 1
strtok nTok3 sLine "node =" 1
endif
Termwrites nTok1
Termwrites nTok2
Termwrites nTok3

endwhile
fclose 1
endproc



[Screen Output]:
cat /vol/patients_db/dicom/dicompeers.cfg |egrep '%|node|port|called|calling' |
fgrep -v #
%ALI PACS
node = nserv0
port = 4000
callingAEtitle = ALI_STORE_SCP
calledAEtitle = DICOM_SCP
%Adac RTP
node = adacp3u1
port = 104
callingAEtitle = ADACRTP_SCP
calledAEtitle = DICOM_SCP
%PACS cube
node = pacscube
port = 4006
callingAEtitle = ADAC_SCU
calledAEtitle = RIMAGE1
%mitra_RIS
node = broker
port = 3320
callingAEtitle = ADAC_SCU
calledAEtitle = BROKER
%ToshibaNM
node = ultra1
port = 104
callingAEtitle = ADAC_SCU
calledAEtitle = DICOM_SCP
%ALI_Broker
node = alibroker
port = 3320
callingAEtitle = broker
calledAEtitle = DICOM_SCP
SUPERUSER@adac:#[2] atatALIALIrv0ALIrv0ALIrv0ALIrv0Adacrv0Adacacp3u1Adacacp3u1Ad
acacp3u1Adacacp3u1PACSacp3u1PACSPACSPACSPACSmitra_RISmitra_RISkmitra_RISkmitra_R
ISkmitra_RISkToshibaNMkToshibaNMToshibaNMToshibaNMToshibaNMALI_BrokerALI_Brokerk
ALI_BrokerkALI_BrokerkALI_BrokerkALI_BrokerkALI_BrokerkALI_Brokerk
[end of screen output]

Any idea how I can get this down to capturing the string just 1 time? My capture file looks good - just like the screen output. -Mike

 
This is happening because your termwrites commands are executed each time through the while loop. The termwrites commands should instead be inside the corresponding if/endif block that the strtok commands are in. I'm not sure where the transmit "^M" command should be, so you may need to look at that particular case as well.
 
Mike,

In the Original Script that is Posted, the Termwrite OutPut is in the Final If Statement which is the Final Pass thru the File. Your Latest Posted Script has the Script Writing the Variables More than one Time. So now I'll ask a few questions. Do you want your data to appear as:

ALI_Broker, alibroker, 3320, broker,DICOM_SCP.

Or has that changed ?? Not sure what you are looking For !! Also you omitted the Strfmt code to Output the Data in a One Liner ..

Let me Know

Hank
 
Almost there. Here is what I'm getting now. It looks like fields 1 and 3 are correct but fields 2, 4 and 5 are truncating for some reason. Also, do I really have 30 string variables here? Can I use them in other scripts? It looks like we only defined 15 data strings. How do we end up with 30?

proc main
string sLine
string hTok1,hTok2,hTok3
string nTok1,nTok2,nTok3
string pTok1,pTok2,pTok3
string gTok1,gTok2,gTok3
string dTok1,dTok2,dTok3
string Output
string szName = "Node_data.cap"

clear
set capture overwrite on
set capture query off
set capture file szName

transmit "^M"
waitfor "]"
clear
transmit "cat /vol/patients_db/dicom/dicompeers.cfg |egrep '%|node|port|called|calling' |fgrep -v #^M"
Capture on
waitfor "]"
Capture off

fopen 1 "C:\program files\procomm plus\capture\Node_data.cap" READ TEXT
while not feof 1
fgets 1 sLine
if strfind sLine "%"
strtok hTok1 sLine "%" 1
strtok hTok2 sLine "%" 1
strtok hTok3 sLine "%" 1
endif

if strfind sLine "node = "
strtok nTok1 sLine "node = " 1
strtok nTok2 sLine "node = " 1
strtok nTok3 sLine "node = " 1
; strdelete nTok1 0 1
endif

if strfind sLine "port = "
strtok pTok1 sLine "port = " 1
strtok pTok2 sLine "port = " 1
strtok PTok3 sLine "port = " 1
endif

if strfind sLine "callingAEtitle = "
strtok gTok1 sLine "callingAEtitle = " 1
strtok gTok2 sLine "callingAEtitle = " 1
strtok gTok3 sLine "callingAEtitle = " 1
endif

if strfind sLine "calledAEtitle = "
strtok dTok1 sLine "calledAEtitle = " 1
strtok dTok2 sLine "calledAEtitle = " 1
strtok dTok3 sLine "calledAEtitle = " 1
strfmt Output "%s,%s,%s,%s,%s" hTok1 nTok1 pTok1 gTok1 dTok1
Termwrites Output
Termwrites "`n`r"
endif
endwhile
fclose 1
transmit "^M"
endproc



[start screen output]
cat /vol/patients_db/dicom/dicompeers.cfg |egrep '%|node|port|called|calling' |
fgrep -v #
%ALI_PACS
node = nserv0
port = 4000
callingAEtitle = ALI_STORE_SCP
calledAEtitle = DICOM_SCP
%Adac_RTP
node = adacp3u1
port = 104
callingAEtitle = ADACRTP_SCP
calledAEtitle = DICOM_SCP
%PACS_cube
node = pacscube
port = 4006
callingAEtitle = ADAC_SCU
calledAEtitle = RIMAGE1
%mitra_RIS
node = broker
port = 3320
callingAEtitle = ADAC_SCU
calledAEtitle = BROKER
%ToshibaNM
node = ultra1
port = 104
callingAEtitle = ADAC_SCU
calledAEtitle = DICOM_SCP
%ALI_Broker
node = alibroker
port = 3320
callingAEtitle = broker
calledAEtitle = DICOM_SCP
SUPERUSER@adac:#[20] ALI_PACS,s,4000,LI_STOR,DICOM_SCP
Adac_RTP,a,104,D,DICOM_SCP
PACS_cube,pacscub,4006,D,RIM
mitra_RIS,br,3320,D,BROK
ToshibaNM,ultra1,104,D,DICOM_SCP
ALI_Broker,alibr,3320,brok,DICOM_SCP
[end screen output]

Once I get these strings correct I need to use them in a string like this (using ALI_Broker as an example):

dicom_echo -c broker -t DICOM_SCP -v alibroker 3320
[variables are: broker, DICOM_SCP, alibroker, 3320]

How would I format this command? Thanks for all your help. Hank and Knob - you guys are great! -Mike
 
I did a little editing of your script and here's what I came up with:

proc main
string sLine
string hTok1,hTok2,hTok3
string nTok1,nTok2,nTok3
string pTok1,pTok2,pTok3
string gTok1,gTok2,gTok3
string dTok1,dTok2,dTok3
string Output
fopen 1 "C:\foo.txt" READ TEXT
while not feof 1
fgets 1 sLine
if strfind sLine "%"
strtok hTok1 sLine "%" 1
endif

if strfind sLine "node = "
strtok nTok1 sLine " = " 2
endif

if strfind sLine "port = "
strtok pTok1 sLine "port = " 1
endif

if strfind sLine "callingAEtitle = "
strtok gTok1 sLine " = " 2
endif

if strfind sLine "calledAEtitle = "
strtok dTok1 sLine " = " 2
strfmt Output "%s,%s,%s,%s,%s" hTok1 nTok1 pTok1 gTok1 dTok1
Termwrites Output
Termwrites "`n`r"
endif
endwhile
fclose 1

endproc

Instead of using the entire string with the strtok command, I just keyed off the equal sign and changed the strtok command to return the second section of the delimited string (i.e. everything after the equal sign). This way you won't need to define all of the variables you are using now, just what is in my modified script above.

If you use either the chain or execute command to launch another script, you can access the variables in your original script as long as you have the same variables defined in the script you chain or execute.

To get your command line formatted properly, you would use the strfmt command. It would look something like this (assuming I understand your environment correctly):

strfmt Command_Line "dicom_echo -c %s -t %s -v %s %s" broker, DICOM_SCP, alibroker, 3320

Keep in mind that the broker, DICOM_SCP, etc. in the above command should be replaced by the different nTok1, pTok1, etc. variables that you are currently using in your script. I didn't want to use those values in my example since my script and your script are outputting the values of different variables at this time.
 
hello hank,

i also work with DMS daily. i was curious if i could please get a copy of your OMPR capture script or any script that illustrates capture from a DMS telnet session? (the .WAS version for educational value?) i can be reached at tommiwarner@hotmail.com

thanks for your time,

tommi
 
knob, that worked perfectly. The delimited strings are all correct now. However when I try the strformat command i get an error: invalid expression token Command_Line and invalid string variable. Here is my syntax:
srtfmt Command_Line "dicom_echo -c %s -t %s -v %s %s" dTok1, gTok1, nTok1, pTok1
Is this the correct syntax?
 
In the command you copied, you have srtfmt instead of strfmt. Double check your script and make sure it is correct there. I compiled the line as you had it in your message and did get the same error message, so I think that's the problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top