|
|
 |
 |
 |
 |
how to remove lines containing 'abc' in a text file?
Umesh wrote: > Please help. THANKS.
Do your own homework. We do not support cheaters
Umesh <fraternitydispo @gmail.com> writes: > Please help. THANKS. No. -- Keith Thompson (The_Other_Keith) k@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
Umesh wrote: > Please help. THANKS.
/* BEGIN abc_remove.c */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #define STRING "abc" #define ARGV_0 "abc_remove" #define INITIAL_BUFFER_SIZE 0 struct list_node { struct list_node *next; void *data; };
int get_line(char **lineptr, size_t *n, FILE *stream); struct list_node *string_node(struct list_node **head, struct list_node *tail, char *string); void list_free(struct list_node *node, void (*free_data)(void *)); int list_fputs(struct list_node *node, FILE *stream); int main(int argc, char *argv[]) { FILE *fp; struct list_node *head, *tail; int rc; char *buff; size_t size; if (argc > 1) { size = INITIAL_BUFFER_SIZE; buff = malloc(size); if (buff == NULL) { size = 0; } while (*++argv != NULL) { fp = fopen(*argv, "r"); if (fp == NULL) { printf("\nfopen() problem with \"%s\"\n", *argv); free(buff); exit(EXIT_FAILURE); } printf("\n%s open for reading.\n", *argv); tail = head = NULL; while ((rc = get_line(&buff, &size, fp)) > 0) { if (strstr(buff, STRING) == NULL) { tail = string_node(&head, tail, buff); if (tail == NULL) { puts("Node allocation failure."); free(buff); fclose(fp); exit(EXIT_FAILURE); } } } if (rc == 0) { puts("realloc returned a null pointer value"); free(buff); fclose(fp); exit(EXIT_FAILURE); } fclose(fp); printf("%s closed.\n", *argv); fp = fopen(*argv, "w"); if (fp == NULL) { printf("\nfopen() problem with \"%s\"\n", *argv); free(buff); exit(EXIT_FAILURE); } printf("%s open for writing.\n", *argv); list_fputs(head, fp); fclose(fp); printf("%s closed.\n", *argv); list_free(head, free); } free(buff); } else { puts("This program removes all lines\n" "containing the string \"" STRING "\", " "from text files" "\n\nUsage:\\" ARGV_0 " <FILE_1.txt> <FILE_2.txt> ..."); } return 0; }
int get_line(char **lineptr, size_t *n, FILE *stream) { int rc; void *p; size_t count; count = 0; while ((rc = getc(stream)) != EOF) { if (count != -1) { ++count; } if (count + 2 > *n) { p = realloc(*lineptr, count + 2); if (p == NULL) { if (*n > count) { if (rc != '\n') { (*lineptr)[count] = '\0'; (*lineptr)[count - 1] = (char)rc; } else { (*lineptr)[count - 1] = '\0'; } } else { if (*n != 0) { **lineptr = '\0'; } ungetc(rc, stream); } count = 0; break; } *lineptr = p; *n = count + 2; } if (rc == '\n') { (*lineptr)[count - 1] = '\0'; break; } (*lineptr)[count - 1] = (char)rc; } if (rc != EOF) { rc = count > INT_MAX ? INT_MAX : count; } else { if (*n > count) { (*lineptr)[count] = '\0'; } } return rc; }
struct list_node *string_node(struct list_node **head, struct list_node *tail, char *string) { struct list_node *node; node = malloc(sizeof *node); if (node != NULL) { node -> next = NULL; node -> data = malloc(strlen(string) + 1); if (node -> data != NULL) { if (*head == NULL) { *head = node; } else { tail -> next = node; } strcpy(node -> data, string); } else { free(node); node = NULL; } } return node; }
void list_free(struct list_node *node, void (*free_data)(void *)) { struct list_node *next_node; while (node != NULL) { next_node = node -> next; free_data(node -> data); free(node); node = next_node; } }
int list_fputs(struct list_node *node, FILE *stream) { while (node != NULL) { if (fputs(node -> data, stream) == EOF) { return EOF; } if (putc('\n', stream) == EOF) { return EOF; } node = node -> next; } return '\n'; }
/* END abc_remove.c */ -- pete
On May 30, 8:12 am, Umesh <fraternitydispo@gmail.com> wrote: > Please help. THANKS.
[OT-answer, for more information go to comp.editors] Open the file in vi, and execute the following command: :g/abc/d [Another OT-asnwer, for more information on this go to comp.unix] Execute the following: grep -v abc <filename> For the implementation in C, please take a look at the sourcecode for grep. Hope this helps [NOT] Kind regards, Johan
On May 30, 12:51 pm, borkh@gmail.com wrote:
> On May 30, 8:12 am, Umesh <fraternitydispo @gmail.com> wrote: > > Please help. THANKS. > [OT-answer, for more information go to comp.editors] > Open the file in vi, and execute the following command: > :g/abc/d > [Another OT-asnwer, for more information on this go to comp.unix] > Execute the following: > grep -v abc <filename> > For the implementation in C, please take a look at the sourcecode for > grep. > Hope this helps [NOT] > Kind regards, > Johan
continuation... There is no command that alters the content of file in Unix except one or two such as dos2unix etc. So filtering is to be done.. grep -v abc filenale > temp.txt // store all lines that doesn't contain abc in temp file mv temp.txt filename // move temp file to source file. Bye Guru Jois
>>>>> "U" == Umesh <fraternitydispo @gmail.com> writes: U> Please help. THANKS. Step 1: Implement perl. Step 2: perl -ni -e 'print if /abc/;' filename Good luck! Charlton -- Charlton Wilbur cwil@chromatico.net
In article <873b1e734k.@mithril.chromatico.net>, Charlton Wilbur <cwil@chromatico.net> wrote: >>>>>> "U" == Umesh <fraternitydispo @gmail.com> writes: > U> Please help. THANKS. >Step 1: Implement perl. >Step 2: perl -ni -e 'print if /abc/;' filename Arguably, that would "select" lines containing 'abc' rather than "remove lines containing 'abc'". There is a meaning of "remove" congruous with "select and place elsewhere", but that meaning of "remove" would normally be signalled with the preposition "to" rather than "in" -- "remove to <a location>" rather than "remove in <something>". -- Programming is what happens while you're busy making other plans.
>>>>> "WR" == Walter Roberson <rober @ibd.nrc-cnrc.gc.ca> writes: WR> In article <873b1e734k.@mithril.chromatico.net>, WR> Charlton Wilbur <cwil@chromatico.net> wrote: >>>>>>> "U" == Umesh <fraternitydispo@gmail.com> writes: U> Please help. THANKS. >> Step 1: Implement perl. >> Step 2: perl -ni -e 'print if /abc/;' filename WR> Arguably, that would "select" lines containing 'abc' rather WR> than "remove lines containing 'abc'". There is a meaning of WR> "remove" congruous with "select and place elsewhere", but that WR> meaning of "remove" would normally be signalled with the WR> preposition "to" rather than "in" -- "remove to <a location>" WR> rather than "remove in <something>". Whoops, you're right. s/if/unless/ in what I wrote. Charlton -- Charlton Wilbur cwil@chromatico.net
|
 |
 |
 |
 |
|