how to remove lines containing 'abc' in a text file?

Discussion in 'C Programming' started by Umesh, May 30, 2007.

  1. Umesh

    Umesh Guest

    Please help. THANKS.
     
    Umesh, May 30, 2007
    #1
    1. Advertising

  2. Umesh

    jacob navia Guest

    Umesh wrote:
    > Please help. THANKS.
    >


    Do your own homework. We do not support cheaters
     
    jacob navia, May 30, 2007
    #2
    1. Advertising

  3. Umesh <> writes:
    > Please help. THANKS.


    No.

    --
    Keith Thompson (The_Other_Keith) <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"
     
    Keith Thompson, May 30, 2007
    #3
  4. Umesh

    pete Guest

    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
     
    pete, May 30, 2007
    #4
  5. Umesh

    Guest

    Re: how to remove lines containing 'abc' in a text file?

    On May 30, 8:12 am, Umesh <> 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
     
    , May 30, 2007
    #5
  6. Umesh

    Guru Jois Guest

    Re: how to remove lines containing 'abc' in a text file?

    On May 30, 12:51 pm, wrote:
    > On May 30, 8:12 am, Umesh <> 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
     
    Guru Jois, May 30, 2007
    #6
  7. >>>>> "U" == Umesh <> writes:

    U> Please help. THANKS.

    Step 1: Implement perl.

    Step 2: perl -ni -e 'print if /abc/;' filename

    Good luck!

    Charlton


    --
    Charlton Wilbur
     
    Charlton Wilbur, May 30, 2007
    #7
  8. In article <>,
    Charlton Wilbur <> wrote:
    >>>>>> "U" == Umesh <> 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.
     
    Walter Roberson, May 30, 2007
    #8
  9. >>>>> "WR" == Walter Roberson <-cnrc.gc.ca> writes:

    WR> In article <>,
    WR> Charlton Wilbur <> wrote:
    >>>>>>> "U" == Umesh <> 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
     
    Charlton Wilbur, May 30, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jiong Feng
    Replies:
    0
    Views:
    910
    Jiong Feng
    Nov 19, 2003
  2. Bruce Sam
    Replies:
    15
    Views:
    8,036
    John C. Bollinger
    Nov 19, 2004
  3. vsgdp

    ABC inheriting from ABC

    vsgdp, Sep 24, 2005, in forum: C++
    Replies:
    1
    Views:
    323
    vsgdp
    Sep 24, 2005
  4. Gunter Henriksen

    x.abc vs x['abc']

    Gunter Henriksen, May 13, 2009, in forum: Python
    Replies:
    1
    Views:
    370
    alex23
    May 15, 2009
  5. Replies:
    4
    Views:
    168
Loading...

Share This Page