Craig
Had a look last night. This fortran code attached will do exactly what you want.
Ron



Craig
Sounds like a job for Perl. Should be straightforward, then BBedit run Perl script if you want to do it in BBedit.
Ron

On 05/08/2016 11:57 AM, Craig Maynard wrote:
I'd like to use BBEdit to periodically do the following:

• Find all occurrences of duplicate lines in my .bash_history file.
• Remove the duplicates and then re-order the remaining lines by frequency, with the most frequent at the end of the file.

If anyone has done this before, can you suggest a good way to proceed?

Thanks,
Craig

--
This is the BBEdit Talk public discussion group. If you have a
feature request or would like to report a problem, please email
"[email protected]" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>

---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected] <mailto:[email protected]>. To post to this group, send email to [email protected] <mailto:[email protected]>.



--
This is the BBEdit Talk public discussion group. If you have a feature request or would like to report a problem, please email
"[email protected]" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>

--- You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
!gfortran -o new.o testbashhist.f95
program testbashhist
integer, parameter                         :: out=0   !=0 for min. output
integer, parameter                         :: maxlinlen=500!max length of a line
integer, parameter                         :: ird=5   !logical no. read terminal
integer, parameter                         :: ipr=6   !logical no. write terminal
integer, parameter                         :: Runit=7 !logical no. of input file
integer, parameter                         :: Wunit=8 !logical no. of output file
integer, allocatable, dimension(:)         :: lenlin  !no. of chars in line
integer, allocatable, dimension(:)         :: num     !no. of times for duplicates
character(len=maxlinlen), allocatable, dimension(:) :: uniqlin !unique lines as read
character(len=maxlinlen)                   :: curlin  !current line as used
character(len=maxlinlen)                   :: curlinx !current line as read
character(len=maxlinlen)                   :: tempfil !filename as read
character(len=maxlinlen)                   :: filename!filename as used
character(len=1)                           :: yn      !query, write a file?
integer                                    :: lines   !no. of lines read
integer                                    :: countlin!no. unique lines
integer                                    :: maxnumlin!number of lines in file
integer                                    :: countdup!count duplicate lines
integer                                    :: dupoint !back pointer for duplicates
integer                                    :: Rerr    !read file error
integer                                    :: Werr    !write file error
logical                                    :: Ropen   !check read file open
logical                                    :: Wopen   !check write file open
integer                                    :: i       !loops over lines read
integer                                    :: j       !loops over unique lines
integer                                    :: k       !print loop variable
integer                                    :: nlins   !no. lines in file
integer                                    :: eof     !end of file code
integer                                    :: l1      !length of filename
integer                                    :: l2      !length of line read
    write (ipr, '(1x,a)', advance='no') 'filename to read :> '
    read (ird,*) tempfil
    tempfil=adjustl(tempfil)
    l1=len_trim(tempfil)
    filename(1:l1)=tempfil(1:l1)
    write (ipr, '(1x,2a)') 'read from filename: ', filename(1:l1)
    open(unit=Runit, iostat=Rerr, file=filename(1:l1), status='old',&
        access='sequential', blank='null', form='formatted', action='read')
    if (Rerr.ne.0) then
        write (ipr, '(1x,3a,i0,a,i0)') 'failed to open file: ',&
                    filename(1:l1), ' on unit ', Runit, 'Rerr=', Rerr
        inquire(unit=Runit, opened=Ropen)
        if (Ropen .eqv. .true.) then
            write (ipr, '(1x,3a,i0,a)') 'file: ', filename(1:l1),&
                        ' on unit ', Runit, ' still open'
            close (unit=Runit, status='keep')      
            write (ipr, '(1x,a6,a,a9,i0,a7)') 'file: ', filename(1:l1),&
                        ' on unit ', Runit, ' closed'
        end if
        stop
    end if
    rewind Runit

    nlins=0
    do
        read (Runit, '(a)', iostat=eof, err=123, end=456) curlin
        if (eof .ne. 0) go to 123
        nlins=nlins+1
    end do
123 print *, 'a read file error has occurred.  ', 'iostat (eof)=', eof
    print *, curlin
    stop
456 write (ipr, '(1x,a25,i0)') 'no. of lines in file is: ', nlins
    rewind Runit
    maxnumlin=nlins
    if (.not. allocated(lenlin))  allocate(lenlin(maxnumlin))
    if (.not. allocated(num))     allocate(num(maxnumlin))
    if (.not. allocated(uniqlin)) allocate(uniqlin(maxnumlin))
    if (out.ne.0) write (ipr, '(1x,a)') 'temporary files allocated'
    curlin=''
    curlinx=''
    i=1
    read  (Runit,'(a)')  curlinx  !this should be less than the maxlinlen=500 parameter
    curlinx=adjustl(curlinx)
    l2=len_trim(curlinx)
    curlin(1:l2)=curlinx(1:l2)
    uniqlin(1)=''
    uniqlin(1)(1:l2)=curlin(1:l2)
    lenlin(1)=len_trim(curlin)
    num(1)=1
    countlin=1
    lines=1
    if (out.ne.0) write (ipr, '(1x,a7,i5,1x,a,2i5)') 'added: ', lines, uniqlin(1)(1:l2), &
                            lenlin(1), num(1)
    do i=2,maxnumlin
        countadd=0
        countdup=0
        curlin=''
        curlinx=''
        read  (Runit,'(a)')  curlinx  !this should be less than the maxlinlen=500 parameter
        curlinx=adjustl(curlinx)
        l2=len_trim(curlinx)
        curlin(1:l2)=curlinx(1:l2)
        lines=lines+1
        do j=1,countlin
          if (curlin(1:l2) .eq. uniqlin(j)(1:lenlin(j))) then
              countdup=countdup+1
              if (countdup.eq.1) dupoint=j
              if (out.ne.0) write (ipr, '(1x,a28,i0,1x,a)') &
                          'duplicate detected at line: ',lines
          else
              countadd=countadd+1
          end if       
        end do
        if (countdup.gt.0) then
            num(dupoint)=num(dupoint)+countdup
        else
            countlin=countlin+1
            uniqlin(countlin)(1:l2)=''
            uniqlin(countlin)(1:l2)=curlin(1:l2)
            lenlin(countlin)=l2
            num(countlin)=1
            if (out.ne.0) write (ipr, '(1x,a,i5,1x,a,2i5)') 'added: ', lines, curlin(1:l2), &
                      lenlin(countlin), num(countlin)
        end if
    end do
    if (out.ne.0) write (ipr, '(1x,2i5,5x,a)') (num(k),lenlin(k),uniqlin(k)(1:lenlin(k)), k=1,countlin)
    if (out.ne.0)write (ipr, '(1x,a25,i0)')  'no. of lines in file is: ', maxnumlin
    write (ipr, '(1x,a23,i0)')  'no of unique lines is: ', countlin
    inquire(unit=Runit, opened=Ropen)
    if (Ropen .eqv. .true.) then
        if (out.ne.0) write (ipr, '(1x,3a,i0,a)') 'file: ', filename(1:l1),&
                                  ' on unit ', Runit, ' still open'
        close (unit=Runit, status='keep')      
        write (ipr, '(1x,3a,i0,a)') 'file: ', filename(1:l1),&
                    ' on unit ', Runit, ' closed'
    end if
    call bubblesort(num, lenlin, uniqlin, countlin)
    write (ipr, '(1x,a)') ' freq length   command'
    write (ipr, '(1x,i5,i7,3x,a)') (num(k),lenlin(k),uniqlin(k)(1:lenlin(k)), k=1,countlin)
    write (ipr, '(1x,a25,i0)')  'no. of lines in file is: ', maxnumlin
    write (ipr, '(1x,a23,i0)')  'no of unique lines is: ', countlin

!now write the new bash history file
666 write (ipr, '(1x,a)', advance='no') 'do you want to save a file? (y or n) :> '
    read (ird,'(a1)') yn
    if (yn.eq.'N' .or. yn.eq.'n' .or. yn.eq.'Q' .or. yn.eq.'q') then
        if (allocated(lenlin))  deallocate(lenlin)
        if (allocated(num))     deallocate(num)
        if (allocated(uniqlin)) deallocate(uniqlin)
        if (out.ne.0) write (ipr, '(1x,a)') 'temporary files deallocated'
        stop
    end if
    if (yn.eq.'Y' .or. yn.eq.'y') then
        write (ipr, '(1x,50a)', advance='no') 'give it a filename :> '
        read (ird,*) tempfil
        tempfil=adjustl(tempfil)
        l1=len_trim(tempfil)
        filename(1:l1)=tempfil(1:l1)
        write (ipr, '(1x,2a)') 'write to filename: ', filename(1:l1)
        open(unit=Wunit, iostat=Werr, err=2222, file=filename(1:l1),&
            status='replace',access='sequential',&
            form='formatted', action='write')
        if (Werr.eq.0) then
            write (Wunit, '(1x,a)') (uniqlin(k)(1:lenlin(k)), k=1,countlin)
            if(out .ne. 0) write (ipr, '(1x,a)') ' freq length   command'
            if(out .ne. 0) write (ipr, '(1x,i5,i7,3x,a)') (num(k),lenlin(k),uniqlin(k)(1:lenlin(k)), k=1,countlin)
            write (ipr, '(1x,a25,i0)')  'no. of lines in file is: ', maxnumlin
            write (ipr, '(1x,a23,i0)')  'no of unique lines is: ', countlin
            close (unit=Wunit, status='keep')      
            write (ipr, '(1x,3a,i0,a)') 'file: ', filename(1:l1),&
                ' on unit ', Wunit, ' closed'
        else
            write (ipr, '(1x,3a,i0,a,i0)') 'failed to open file: ',&
                        filename(1:l1), ' on unit ', Wunit, 'Werr=', Werr
        end if
 2222   inquire(unit=Wunit, opened=Wopen)
        if (Wopen .eqv. .true.) then
            if (out.ne.0) write (ipr, '(1x,3a,i0,a)') 'file: ', filename(1:l1),&
                                      ' on unit ', Wunit, ' still open'
            close (unit=Wunit, status='keep')      
            write (ipr, '(1x,3a,i0,a)') 'file: ', filename(1:l1),&
                        ' on unit ', Wunit, ' closed'
            stop
        end if
    else
        write (ipr, '(a)') 'Try again'
        go to 666
    end if
    if (allocated(lenlin))  deallocate(lenlin)
    if (allocated(num))     deallocate(num)
    if (allocated(uniqlin)) deallocate(uniqlin)
    if (out.ne.0) write (ipr, '(1x,a)') 'temporary files deallocated'

contains

subroutine bubblesort(list, list2, list3, n)
! list  - integer array holding values to be sorted
! list2 - integer array to sort allong with list
! list3 - character array to sort along with list
! n     - integer number of values in list
integer, intent(inout), dimension(*) :: list, list2
character(len=*), intent(inout), dimension(*) :: list3
integer, intent(in) :: n
integer :: temp
character(len=maxlinlen) :: string
integer :: first, pass
logical :: noexch
pass=1
noexch=.false.
do while (.not. noexch)
noexch=.true.
do first=1,n-pass
if (list(first) .lt. list(first+1)) then
temp=list(first)
list(first)=list(first+1)
list(first+1)=temp
temp=list2(first)
list2(first)=list2(first+1)
list2(first+1)=temp
string=list3(first)
list3(first)=list3(first+1)
list3(first+1)=string
noexch=.false.
end if
end do
pass=pass+1
end do
return
end subroutine bubblesort
end program testbashhist

Reply via email to