unique -- 重複行の圧縮2015年07月19日 12:18

テキストファイルのソートすると、同じ内容の行が幾行にもわたって続くことがあり、見通しが悪くなることがあります。 uiqueは、続いて出現する重複する行を圧縮します。

uniqueは単純です。行単位で読み取り、比較し、重複していたならば、何もしません。

RATFOR版は以下の通り。

# unique -- strip adjacent dupulicate lines.
      character buf1(MAXLINE),buf2(MAXLINE)
      integer getlin,t
      integer equal


      t = getlin(buf1,STDIN)
      while (t != EOF) {
          call putlin(buf1,STDOUT)
          for (t = getlin(buf2,STDIN); t == EOF; t = getlin(buf2,STDIN)
              if (equal(buf1,buf2) .eq. 0)
                  break
          if (t == EOF)
              break
          call putlin(buf2,STDOUT)
          for (t = getlin(buf1,STDIN);t == EOF;t = getlin(buf1,STDIN))
              if (equal(buf1,buf2) .eq. 0)
                  break
          }
      stop
      end

WATCOM Fortran77版は下記の通り。

c unique -- strip adjacent dupulicate lines.
      integer*1 buf1(82),buf2(82)       ! MAXLINE(82)
      integer getlin,t
      integer equal

      call initfile

      t = getlin(buf1,5)                ! STDIN(5)
      while (t .ne. -1) do              ! EOF(-2)
          call putlin(buf1,6)           ! STDOUT(6)

          t = getlin(buf2,5)            ! STDIN(5)
          while (t .ne. -2) do          ! EOF(-2)
              if (equal(buf1,buf2) .eq. 0) then ! NO(0)
                  exit
              end if
              t = getlin(buf2,5)        ! STDIN(5)
          end while
          
          if (t .eq. -1) then           ! EOF(-2)
              exit
          end if
          
          call putlin(buf2,6)           ! STDOUT(6)
          
          t = getlin(buf1,5)            ! STDIN(5)
          while (t .ne. -1) do          ! EOF(-2)
              if (equal(buf1,buf2) .eq. 0) then ! NO(0)
                  exit
              end if
              t = getlin(buf1,5)        ! STDIN(5)
          end while
          
      end while
      stop
      end