By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,440 Members | 1,420 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,440 IT Pros & Developers. It's quick & easy.

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

P: n/a
Please help. THANKS.

May 30 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Umesh wrote:
Please help. THANKS.
Do your own homework. We do not support cheaters

May 30 '07 #2

P: n/a
Umesh <fr****************@gmail.comwrites:
Please help. THANKS.
No.

--
Keith Thompson (The_Other_Keith) ks***@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"
May 30 '07 #3

P: n/a
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
May 30 '07 #4

P: n/a
On May 30, 8:12 am, Umesh <fraternitydispo...@gmail.comwrote:
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 '07 #5

P: n/a
On May 30, 12:51 pm, borkh...@gmail.com wrote:
On May 30, 8:12 am, Umesh <fraternitydispo...@gmail.comwrote:
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

May 30 '07 #6

P: n/a
>>>>"U" == Umesh <fr****************@gmail.comwrites:

UPlease help. THANKS.

Step 1: Implement perl.

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

Good luck!

Charlton
--
Charlton Wilbur
cw*****@chromatico.net
May 30 '07 #7

P: n/a
In article <87************@mithril.chromatico.net>,
Charlton Wilbur <cw*****@chromatico.netwrote:
>>>>>"U" == Umesh <fr****************@gmail.comwrites:
UPlease 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.
May 30 '07 #8

P: n/a
>>>>"WR" == Walter Roberson <ro******@ibd.nrc-cnrc.gc.cawrites:

WRIn article <87************@mithril.chromatico.net>,
WRCharlton Wilbur <cw*****@chromatico.netwrote:
>>>>>>"U" == Umesh <fr****************@gmail.comwrites:
UPlease help. THANKS.
>Step 1: Implement perl.
>Step 2: perl -ni -e 'print if /abc/;' filename
WRArguably, that would "select" lines containing 'abc' rather
WRthan "remove lines containing 'abc'". There is a meaning of
WR"remove" congruous with "select and place elsewhere", but that
WRmeaning of "remove" would normally be signalled with the
WRpreposition "to" rather than "in" -- "remove to <a location>"
WRrather than "remove in <something>".

Whoops, you're right.

s/if/unless/ in what I wrote.

Charlton
--
Charlton Wilbur
cw*****@chromatico.net
May 30 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.