Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for unique.rex

(view source code of unique.rex as plain text)

  1. /* Remove double lines from a text file */
  2.  
  3. /* Check command line arguments */
  4. Parse Arg clargs
  5. If clargs = "" Then Call Syntax
  6. If Pos( "?", clargs ) > 0 Then Call Syntax
  7. Parse Value clargs With source target dummy1"/"switch dummy2
  8. Select
  9. 	When dummy1 <> "" Then Call Syntax
  10. 	When dummy2 <> "" Then Call Syntax
  11. 	When switch <> "" Then Do
  12. 		If Translate( switch ) = "I" Then case = 0; Else Call Syntax
  13. 	End
  14. 	Otherwise case = 1
  15. End
  16. If target = "" Then target = source
  17.  
  18. /* Hide standard error */
  19. "@ECHO OFF"
  20. Trace Off
  21.  
  22. /* Check if specified files exist */
  23. If Stream( source, "C", "QUERY EXISTS" ) = "" Then Call Syntax
  24.  
  25. /* Read the source file */
  26. "CLS"
  27. Say "Reading source file . . ."
  28. If LineIn( source, 1, 0 ) = "NOTREADY" then Do
  29. 	Say
  30. 	Say "Error opening source file "||source
  31. 	Call Syntax
  32. End
  33. Do i = 1 By 1 Until Lines( source ) = 0
  34. 	source.i = LineIn( source )
  35. 	If case = 0 Then Do
  36. 		source.i = translate( source.i, "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ" )
  37. 	End
  38. 	source.0 = i
  39. End
  40. Call Stream source, "C", "CLOSE"
  41.  
  42. /* Remove duplicate lines */
  43. "CLS"
  44. Say "Searching "||source.0||" lines for duplicates . . ."
  45. target.  = ""
  46. target.0 = 0
  47. Do i = 1 By 1 To source.0
  48. 	"CLS"
  49. 	Say "Searching "||source.0||" lines for duplicates . . ."
  50. 	Say "Checking line "||i||" . . ."
  51. 	duplicate = 0
  52. 	If target.0 > 0 Then Do j = 1 By 1 To target.0
  53. 		If source.i = target.j Then Do
  54. 			duplicate = 1
  55. 			Leave j
  56. 		End
  57. 	End
  58. 	If duplicate = 0 Then Do
  59. 		k = target.0 + 1
  60. 		target.k = source.i
  61. 		target.0 = k
  62. 	End
  63. End
  64.  
  65. /* Release some memory */
  66. source. = ""
  67.  
  68. /* Save results */
  69. "CLS"
  70. Say "Saving results ("||target.0||" lines) . . ."
  71. "DEL "||target||" >NUL 2>&1"
  72. Do i = 1 By 1 To target.0
  73. 	Call LineOut target, target.i
  74. End
  75. Call LineOut target
  76.  
  77. /* Done */
  78. Exit 0
  79.  
  80.  
  81.  
  82.  
  83. Syntax: Procedure
  84. 	Say
  85. 	Say "Unique.rex, Version 1.00 for Regina Rexx"
  86. 	Say "Remove repeating lines from the specified text file"
  87. 	Say
  88. 	Say "Usage:  REGINA.EXE  UNIQUE.REX  sourcefile  [ targetfile ]  [ /I ]"
  89. 	Say
  90. 	Say "Where:  sourcefile  is the file to be checked on repeated lines"
  91. 	Say "        targetfile  is the file to which the result is written; the target file"
  92. 	Say "                    is purged before the result is stored; if no target file is"
  93. 	Say "                    specified, then the source file itself will be modified"
  94. 	Say "         /I         case-insensitive comparisons; results will be all lowercase"
  95. 	Say
  96. 	Say "Note:   This Rexx script was written to check and modify small text files;"
  97. 	Say "        I also tested it with an 8000 line file with 5500 duplicate lines;"
  98. 	Say "        although it does work, with file sizes like that it is really slow!"
  99. 	Say
  100. 	Say "Written by Rob van der Woude"
  101. 	Say "http://www.robvanderwoude.com"
  102. 	Exit 1
  103. Return
  104.  

page last modified: 2024-02-26; loaded in 0.0258 seconds