Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi All,
I need to create a script which search some data in an existing file and then write the updated data in the new file e.g. if in file OLD some texthi
how are youis there then in the new file i want to write
Hello Sir
How are you?Thanks in advance.

It depends on the search criteria to decide which method can be used to decide if they are fullfilled:
If You just want the strings "hi" and "how are you" or both somewhere in the file, then a simple DCL
$ search oldfile "hi","how are you"
$ if $severity .eq.1
$ then
$ copy sys$input newfile
Hello Sir
How are you?
$ endif
will do it.If You require the strings to be both present, and in one line, then add /MATCH=AND to the SEARCH command.
Other requirements like having both strings, but on different lines, may be harder to achieve with a simple search: You have to READ the file line by line, and use the lexical function F$LOCATE to see if it contains the search string.
Probably Perl would be the better tool to do pattern matching: ask in the ITRC forum (see my signature) , and I'm sure You will get a perl solution.
Joseph Huber, http://www.huber-joseph.de
ITRC

Thanks Joseph, this worked ans helped me. Now in the same file i've made the changes and need some different output:
infut file is:Name: ABC
City: xyzHello Mike you are going to USA
Hello James you are going to Europe
Hi Joseph you are going to Africa
-----------------------------------
Hi Allen you are going to Australiaand the output file should be
Name: ABC
City: xyzHey Mike, go to "USA"
Hey James, go to "Europe"
Hey Joseph, go to "Africa"
-----------------------------------------
Hey Allen, go to "Australia"Also i want to take USA, Europe as parameters so that it can be changed.
Thanks in advance.

Well I will not go and make the full program for You, it is a good exercise in DCL programming.
Something alongside the following:
$! p1=input file p2=outputfile
$ infile="travelin.dat"
$ outfile="travelout.dat"
$ if p1.nes."" then infile=p1
$ if p2.nes."" then outfile=p1
$ country="" !default take all countries
$ if p3.nes."" then country=p3
$ open/read/error=nofile in 'infile' !<- your input filename here
$ open/write/error=openerr out 'outfile' !<- your output filename here
$ Q="""
$loop:
$ read/error=readerr/end=done in line
$ line=f$edit(line,"COMPRESS") !reduce multiple white space
$ key=f$element(0," ",line)
$ skip=0
$ if key.eqs."Hello" .or. key.eqs."Hi"
$ then
$ name=f$element(1," ",line) !get name
$ dest=f$element(6," ",line) !get destination country
$ if country.nes."" .and. dest.nes.country then skip=1
$ line="Hey "+name+", go to "+Q+dest+Q
$ endif
$ if .not.skip then write out line
$ goto loop
$done:
$ close in
$ close out
$ exit
$readerr:
$ write sys$output "Error in reading from input file ''infile'!"
$ close in
$ close out
$ EXIt
$nofile:
$ write sys$output "can't open input file ''infile'!"
$ exit
$openerr:
$ write sys$output "error opening output file ''outfile''!"
$ close in
$ exit
Execute the file with
@thisscript myinput.dat myoutput.dat
or
@thisscript myinput.dat myoutput.dat "Europe"
to select only the European destinations.Joseph Huber, http://www.huber-joseph.de
ITRC

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |