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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Text

Status
Not open for further replies.

nguyenb9

Technical User
Apr 1, 2003
55
US
I need help on insert, here it is:

1. I have an input text file consists of the following:
>TUPLE ADDED
>TUPLE ALREADY EXISTS

2. Then I have an existing out file consists of the following:
123456789:test:hello
123456789:test:hello


2. I would like to have a procomm script to read the input text file and insert into the ouput file. Example, if "TUPLE ADDED" found then insert and "TUPLE ALREADY EXISTS" then insert

123456789:test:hello:OK:TUPLE ADDED
123456789:test:hello:FAILED:TUPLE ALREADY EXIST

else if "TUPLE ADDED or TUPLE ALREADY EXISTS" not found
then
123456789:test:hello:FAILED
123456789:test:hello:FAILED

Thanks for your help.
 
This should work.

Code:
string GoodString  = ">TUPLE ADDED"
string BadString   = ">TUPLE ALREADY EXISTS"
string InputFile   = "input.txt"
string OutputFile  = "output.txt"


proc main
string Source
integer SourceLen
fopen 1 OutputFile readwrite text
   fopen 2 InputFile read text
      while not feof 2
         fgets 1 Source
         fgets 2 Source
         strlen Source SourceLen
         if SourceLen > 0
            fseek 1 (-2) 1
            if strcmp Source GoodString
               finsblock 1 15
               fwrite 1 ":OK:TUPLE ADDED" 15
            elseif strcmp Source BadString
               finsblock 1 27
               fwrite 1 ":FAILED:TUPLE ALREADY EXIST" 27
            else
               finsblock 1 7
               fwrite 1 ":FAILED" 7
            endif
            fseek 1 2 1
         endif
      endwhile
   fclose 2
fclose 1
endproc
 
Thank you for your help. But not yet right. When I ran the script, the output is like:

123456789:test:hello:FAILED
123456789:test:hel:FAILED:FAILED:FAILEDlo

I would like to have the output to look like:

123456789:test:hello:OK:TUPLE ADDED
123456789:test:hello:FAILED:TUPLE ALREADY EXIST

Thank you
 
There are a couple of reasons why the script gave the output it did.

[li]The input file had 4 lines of data (2 more than the output file) and none of these lines met the required criteria.[/li]
[li]The last line on the output file did not have a carriage return.[/li]

By using this ammended script, you should not get the last line error or the different line number problem, but it still appears that the lines of data on your input file do not match the criteria in the script (GoodString & BadString).

Code:
string GoodString  = ">TUPLE ADDED"
string BadString   = ">TUPLE ALREADY EXISTS"
string InputFile   = "input.txt"
string OutputFile  = "output.txt"


proc main
string Source
integer SourceLen
fopen 1 OutputFile readwrite text
   fopen 2 InputFile read text
      while not feof 1
         fgets 1 Source
         fgets 2 Source
         strlen Source SourceLen
         if SourceLen > 0
            if not feof 1
               fseek 1 (-2) 1
            endif
            if strcmp Source GoodString
               finsblock 1 15
               fwrite 1 ":OK:TUPLE ADDED" 15
            elseif strcmp Source BadString
               finsblock 1 27
               fwrite 1 ":FAILED:TUPLE ALREADY EXIST" 27
            else
               finsblock 1 7
               fwrite 1 ":FAILED" 7
            endif
            fseek 1 2 1
         endif
      endwhile
   fclose 2
fclose 1
endproc

If you still get the error, ensure that the input file is actually an ascii text file and the criteria you are looking for
[li]>TUPLE ADDED[/li]
[li]>TUPLE ALREADY EXISTS[/li]
is exactly correct(string length, case, spaces, etc). Try creating a new input file using your aspect editor and add the 2 lines exactly as they appear above.
 
How do I convert a file in procomm script to ascii?

Thanks,
 
Opening the file in Procomm's Aspect editor and saving it as a ".txt" file should do the trick.
 
Instead of using procomm and save as .txt, do you have a way that you can put it in the script?
I would like to extract a field from a file then insert a field next to the extracted field. For example:

Input file:
1234567:test1:test2:abc

Extracted:
1234567:test1

Insert different field:
1234567:test1:insertfield

Thanks,

 
If your input file is not already a standard text file then trying to read it from Procomm may produced a lot of control characters within the text and you may not get the desired results. I do not know if Procomm has any means of converting other format files(ie.*.pdf;*.doc;*.rtf) to text files so you will need some other medium to do this.


As for extracting and manipulating the existing data in either the output or input files, you need to understand what the posted script is doing. The script already opens the output and input files as text.
Code:
fopen 1 OutputFile readwrite text ; Open Output as File ID 1
fopen 2 InputFile read text       ; Open Input as File ID 2
While not at the end of the output file,
Code:
while not feof 1
extract line from each file stored as string var Source.
Code:
fgets 1 Source  ; Extract Line from Output File ID 1
fgets 2 Source  ; Extract Line from Input File  ID 2
At this point, if you are not at the end of the document, the file pointer will be at the begining of the next line. To append the previous line, we need to move the file pointer 2 bytes back to allow for the C/R.
Code:
fseek 1 (-2) 1 ; Set pointer of File ID 1 back 2 bytes
Then, depending of the contents of the Output string
Code:
if strcmp Source GoodString     
elseif strcmp Source BadString
We insert a block of space the same size as the string of information we want to insert
Code:
finsblock 1 15 ; Insert 15 Bytes of space into File ID 1
then we insert the sting.
Code:
fwrite 1 ":OK:TUPLE ADDED" 15 ; Write 15 Bytes to File ID 1
After inserting the data, we need to move the file pointer 2 bytes forward to place us back at the begining of the next line.
Code:
fseek 1 2 1 ; Set pointer of File ID 1 forward 2 bytes
before extracting the next lines of data.


Currently, the script does not do anything to the line it extracts from the output file. It only extract a line from the file to ensure file pointer is on the same line in both files. The line extracted from the input file uses strcmp to see if exactly matches the specified criteria.
Code:
if strcmp Source GoodString     
elseif strcmp Source BadString
You could use strfind instead of strcmp to search for the existence of a string in the extracted line.
Code:
if strfind Source GoodString
or to find the position of GoodString in Source
Code:
strfind Source GoodString StrPos
You could use substr to extract specified number of characters from a string.
Code:
substr Target Source 0 10 ; Extract first 10 chars of Source to Target
To delete a string from a file, you can use strlen to determine the length of the string
Code:
strlen Source SrcLen ; Length of Source stored to int Srclen
and then fdelblock to delete the string.
Code:
fdelblock 1 SrcLen ; Delete SrcLen Bytes from File ID 1
NOTE: You will need to make sure the file pointer is at the begining of the string you want to delete before running fdelblock.
Code:
fseek 1 (-10) 1 ; Set pointer of File ID 1 back 10 bytes
 
As for manipulating the data in the example you gave previously, You can use the strextract command to extract the fields you want and strfmt command to insert the new fields. See example below.

Code:
proc main
string ReplacementLine, Field1, Field2
string Input  = "1234567:test1:test2:abc"
string Field3 = "insertfield"

;Extract 1st field to Field1 
strextract Field1 Input ":" 0   

;Extract 2nd field to Field2
strextract Field2 Input ":" 1   

;Format replacement string
strfmt ReplacementLine "%s:%s:%s" Field1 Field2 Field3

usermsg ReplacementLine
endproc
 
Thank you for responding. Can you analyze a script below. The script is written by someone else and I customized it and it came close to the result that I wanted to be. Here it is:

Following files:
SASTXIN.TXT:
1234567:172.17.48.260:table aniscusp:add 2144201005^M ^M 1234567:172.17.48.260:table aniscusp:add 2144201005^M ^M

SASTXOUT.TXT:
TUPLE ALREADY EXISTS
TUPLE ALREADY EXISTS

When I ran a script below, the output (transout.txt) look like this:
1234567:172.17.48.260:
1234567:172.17.48.260: FAILED:TUPLE ALREADY EXIST
FAILED:TUPLE ALREADY EXIST

But I want the Output to look like this:
1234567:172.17.48.260: FAILED:TUPLE ALREADY EXIST
1234567:172.17.48.260: FAILED:TUPLE ALREADY EXIST

SCRIPT:

string GoodString = ">TUPLE ADDED"
string BadString = "TUPLE ALREADY EXISTS"
string DelString = ">TUPLE DELETED"
string NotString = ">TUPLE NOT FOUND"
string InputFile = "sastxout.txt"
string OutputFile = "transout.txt"
string Source
integer SourceLen
string sFind = "172" ;* Search String
string sFile = "C:\sas\sastxin.txt" ;* Source File
string dFile = "C:\sas\transout.txt" ;* Destination File
string sLine ;* Line String Variable
;string sOrder = "0,1,2,3" ;* Output Order / Field
string sOrder = "0,1"
;integer END = 0 ;* End Field
integer END = 2 ;* End Field
string IFS = ":" ;* Input Field Separator
string OFS = ":" ;* Output Field Separator
string sTok, sTok1, snum, sOut ;* String Variables
integer x, y, sl, num ;* Integer Variables
string Fields[22] ;* 15 Element Array

proc Main
DoTheArray()
Endproc

Proc DoTheArray
Fopen 10 sFile Read Text ;* Open Input File
Fopen 11 dFile Create text ;* Open Output File
while not feof 10 ;* While Loop 1
fgets 10 sLine
if strfind sLine sFind ;* Search for string
while 1 ;* While Loop 2
strextract sTok sLine IFS x ;* Extract Field 0
if not NULLSTR sTok ;* NOT End of Line
Fields[x] = sTok ;* LOAD Field into Array
x++ ;* Increment Extract Integer
else
exitwhile ;* Exit While Loop 2
endif
endwhile ;* End While Loop 2
while 1 ;* While Loop 3
strextract snum sOrder "," y ;* Extract sOrder Element
if not NULLSTR snum
strtonum snum num ;* Convert to Number
if num != END ;* NOT END Flag
sTok1 = Fields[num] ;* Compare with sOrder
strfmt sOut "%s%s%s" sTok1 OFS ;* Format Field
;fwrite 11 0x0A0D
strlen sOut sl ;* Length of Field
fwrite 11 sOut sl ;* Write to Destination File
y++ ;* Increment Extract Integer
elseif num == END ;* END Flag Found
sTok1 = Fields[num] ;* LOAD Array Element into Variable
strlen sTok1 sl
fwrite 11 sTok1 sl
y++
endif
else
exitwhile ;* Exit While Loop 3
endif
endwhile ;* End While Loop 3
fputs 11 " " ;* Send NULLSTR to Destination File
for x = 0 upto 21 ;* Reinitialize Array Elements
Fields[x] = " "
endfor
x = 0 ;* Reset Extract Integer
y = 0 ;* Reset Extract Integer
endif
endwhile ;* End While Loop 1
fclose 10
fclose 11

fopen 2 OutputFile readwrite text
fopen 3 InputFile read text
while not feof 3
fgets 2 Source
fgets 3 Source
strlen Source SourceLen
if SourceLen > 0
fseek 2 (-2) 2
if strcmp Source GoodString
finsblock 2 15
fwrite 2 "OK:TUPLE ADDED" 15
endif
if strcmp Source BadString
finsblock 2 27
fwrite 2 "FAILED:TUPLE ALREADY EXIST" 27
endif
if strcmp Source DelString
finsblock 2 17
fwrite 2 "OK:TUPLE DELETED" 17
endif
if strcmp Source NotString
finsblock 2 23
fwrite 2 "FAILED:TUPLE NOT FOUND" 23
endif
; else
; finsblock 2 7
; fwrite 2 "FAILED" 7

fseek 2 3 2
endif
endwhile
fclose 3
fclose 2
pwexit
; endproc
Endproc


THANK YOU FOR ALL YOU HELP.
 
Your script deals with 4 files and you have only posted the contents of two of them, so I can't say what is wrong. However, from the output you are getting I would say that the required output is correct but the position isn't. A couple of things that may be causing your problem.
[ul]
[li]There are C/R N/L control characters within the text[/li]
[li]There are blank lines somewhere within one or more of the text files.[/li]
[/ul]
^M (or Ctrl-M) is the control sequence for a carriage return which normally does not appear in text. You should try and remove any C/R N/L control characters from the string before dealing with it. You can use the strreplace command to do this.

strreplace Source "`r" ""
strreplace Source "`n" ""

If there are blank lines within any of the text files, they should be remove or ignore by the script.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top