Skip to content
/ fr Public

Fast and easy find and replace tool for large files.

License

Notifications You must be signed in to change notification settings

binaryf/fr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 

Repository files navigation

fr-tool

  • An Easy to use find and replace tool for large files.
  • Bugfree.
  • Fork it if needed.

Source

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>

int main(int argc, char **argv) {

    if(argc == 4) {

        int fd = open(argv[1], O_RDONLY);

        if(fd == -1) {
            fprintf(stderr, "Input file not found.\n");
            exit(EXIT_FAILURE);
        }

        char b = 0;
        int index = 0;

        while(read(fd, &b, 1)) {

            if(b == argv[2][index]) {
                ++index;
            }
            else {
                if(index > 0) {

                    off_t o = lseek(fd, 0, SEEK_CUR);
                    lseek(fd, o-index-1, SEEK_SET);
                    
                    while(index > 0) {
                        read(fd, &b, 1);
                        fprintf(stdout, "%c", b);
                        --index;
                    }
                }
                else {
                    fprintf(stdout, "%c", b);
                }
            }
            if(argv[2][index] == 0) {
                fprintf(stdout, "%s", argv[3]);
                index = 0;
            }
        }
        close(fd);
    }
    else {
        fprintf(stdout, "Usage: fr <file> 'from' 'to'\n");
        fprintf(stdout, "Example: ./fr main.c while 'HELLO WORLD!' > out.c\n");
    }
    exit(EXIT_SUCCESS);
}

Syntax

$ fr file 'from' 'to'
$ fr file from to

Usage example

$ fr fr.c while 'HELLO WORLD!' > out.c

Install

$ gcc -o fr fr.c
$ sudo mv fr /usr/local/bin/

About

Fast and easy find and replace tool for large files.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages