Computing.Net > Forums > OpenVMS > Create a new file with text in existing file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Create a new file with text in existing file

Reply to Message Icon

Name: Gargyboy
Date: October 8, 2009 at 12:12:56 Pacific
OS: Windows 2000
CPU/Ram: P4/512
Subcategory: General
Comment:

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 text

hi
how are you

is there then in the new file i want to write

Hello Sir
How are you?

Thanks in advance.



Sponsored Link
Ads by Google

Response Number 1
Name: Joseph.Huber
Date: October 8, 2009 at 23:47:15 Pacific
Reply:

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


1

Response Number 2
Name: Gargyboy
Date: October 9, 2009 at 12:33:22 Pacific
Reply:

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: xyz

Hello 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 Australia

and the output file should be

Name: ABC
City: xyz

Hey 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.


0

Response Number 3
Name: Joseph.Huber
Date: October 9, 2009 at 14:30:27 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Create a new file with text in existing file

Add header in a binary file in Open www.computing.net/answers/openvms/add-header-in-a-binary-file-in-open/525.html

editing a txt file using a com file www.computing.net/answers/openvms/editing-a-txt-file-using-a-com-file/518.html

append a file www.computing.net/answers/openvms/append-a-file/196.html