Executes sed on a specific file

    Args:
        fpath (str):  file path string
        regexpr (str):
        repl (str):
        force (bool): (default = False)
        verbose (bool):  verbosity flag(default = True)
        veryverbose (bool): (default = False)

    Returns:
        list: changed_lines

    CommandLine:
        python -m utool.util_path --exec-sedfile --show

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_path import *  # NOQA
        >>> import utool as ut
        >>> fpath = ut.get_modpath(ut.util_path)
        >>> regexpr = 'sedfile'
        >>> repl = 'saidfile'
        >>> force = False
        >>> verbose = True
        >>> veryverbose = False
        >>> changed_lines = sedfile(fpath, regexpr, repl, force, verbose, veryverbose)
        >>> result = ('changed_lines = %s' % (ut.repr3(changed_lines),))
        >>> print(result)

def sedfile(fpath, regexpr, repl, force=False, verbose=True, veryverbose=False):
    """
    Executes sed on a specific file

    Args:
        fpath (str):  file path string
        regexpr (str):
        repl (str):
        force (bool): (default = False)
        verbose (bool):  verbosity flag(default = True)
        veryverbose (bool): (default = False)

    Returns:
        list: changed_lines

    CommandLine:
        python -m utool.util_path --exec-sedfile --show

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_path import *  # NOQA
        >>> import utool as ut
        >>> fpath = ut.get_modpath(ut.util_path)
        >>> regexpr = 'sedfile'
        >>> repl = 'saidfile'
        >>> force = False
        >>> verbose = True
        >>> veryverbose = False
        >>> changed_lines = sedfile(fpath, regexpr, repl, force, verbose, veryverbose)
        >>> result = ('changed_lines = %s' % (ut.repr3(changed_lines),))
        >>> print(result)
    """
    # TODO: move to util_edit
    path, name = split(fpath)
    new_file_lines = []

    if veryverbose:
        print('[sedfile] fpath=%r' % fpath)
        print('[sedfile] regexpr=%r' % regexpr)
        print('[sedfile] repl=%r' % repl)
        print('[sedfile] force=%r' % force)

    import utool as ut
    file_lines = ut.readfrom(fpath, aslines=True, verbose=False)
    # with open(fpath, 'r') as file:
    #     import utool
    #     with utool.embed_on_exception_context:
    #         file_lines = file.readlines()
    # Search each line for the desired regexpr
    new_file_lines = [re.sub(regexpr, repl, line) for line in file_lines]

    changed_lines = [(newline, line)
                     for newline, line in zip(new_file_lines, file_lines)
                     if  newline != line]
    n_changed = len(changed_lines)
    if n_changed > 0:
        rel_fpath = relpath(fpath, os.getcwd())
        print(' * %s changed %d lines in %r ' %
              (['(dry-run)', '(real-run)'][force], n_changed, rel_fpath))
        print(' * --------------------')
        import utool as ut
        new_file_lines = ut.lmap(ut.ensure_unicode, new_file_lines)
        new_file = ''.join(new_file_lines)
        #print(new_file.replace('\n','\n))
        if verbose:
            if True:
                import utool as ut
                old_file = ut.ensure_unicode(
                    ''.join(ut.lmap(ut.ensure_unicode, file_lines)))
                ut.print_difftext(old_file, new_file)
            else:
                changed_new, changed_old = zip(*changed_lines)
                prefixold = ' * old (%d, %r):  \n | ' % (n_changed, name)
                prefixnew = ' * new (%d, %r):  \n | ' % (n_changed, name)
                print(prefixold + (' | '.join(changed_old)).strip('\n'))
                print(' * ____________________')
                print(prefixnew + (' | '.join(changed_new)).strip('\n'))
                print(' * --------------------')
                print(' * =====================================================')
        # Write back to file
        if force:
            print(' ! WRITING CHANGES')
            ut.writeto(fpath, new_file)
            # with open(fpath, 'w') as file:
            #     file.write(new_file.encode('utf8'))
        else:
            print(' dry run')
        return changed_lines
    #elif verbose:
    #    print('Nothing changed')
    return None