URGENT fast answer needed

A

A. N. O. N.

Hello friends

I need to write a shell utility. It will read the contents of a file,
perform a string replacement on those contents, and print out the result.
It will work similarly to grep except that it performs string replacement
in addition to matching, and it does not support regular expressions or
reading from standard input. But because of the similarities, our command
will be called strep (not to be confused with "strep throat").

Here is how the strep command works. Imagine a file named test.txt
contains the following lines:

foo a b c d foo e
A foo B
0 foo 1 2 foo

We could replace all occurrences of foo with goo as follows (assume we
are in the same directory containing foo.txt):

bash$ strep foo goo test.txt
goo a b c d goo e
A goo B
0 goo 1 2 goo
bash$

There are a couple of things that should be noted. First, the arguments
to strep are passed via the command line and can be accessed through the
variable argv in main. The user must enter three arguments after the
command. The first argument is the string to match (e.g., foo), the
second is the replacement string (e.g., goo), and the third is the file
to read from (e.g., test.txt).

The second thing to note with regard to the example above is that strep
replaces all occurrences of foo with goo on a particular line, not just
the first occurrence (although it will not replace a single string that
spans multiple lines).

Please send me full code for strep.c ASAP thanks.

PS this is not homework
 
U

user923005

Hello friends

I need to write a shell utility. It will read the contents of a file,
perform a string replacement on those contents, and print out the result.
It will work similarly to grep except that it performs string replacement
in addition to matching, and it does not support regular expressions or
reading from standard input. But because of the similarities, our command
will be called strep (not to be confused with "strep throat").

Here is how the strep command works. Imagine a file named test.txt
contains the following lines:

foo a b c d foo e
A foo B
0 foo 1 2 foo

We could replace all occurrences of foo with goo as follows (assume we
are in the same directory containing foo.txt):

bash$ strep foo goo test.txt
goo a b c d goo e
A goo B
0 goo 1 2 goo
bash$

There are a couple of things that should be noted. First, the arguments
to strep are passed via the command line and can be accessed through the
variable argv in main. The user must enter three arguments after the
command. The first argument is the string to match (e.g., foo), the
second is the replacement string (e.g., goo), and the third is the file
to read from (e.g., test.txt).

The second thing to note with regard to the example above is that strep
replaces all occurrences of foo with goo on a particular line, not just
the first occurrence (although it will not replace a single string that
spans multiple lines).

Please send me full code for strep.c ASAP thanks.

PS this is not homework

Enough sed
 
P

Phil Carmody

A. N. O. N. said:
I need to write a shell utility. It will read the contents of a file,
perform a string replacement on those contents, and print out the result. ....
Please send me full code for strep.c ASAP thanks.

Trivial - just remove some of the lines from sed.c.

Phil
 
P

Paul

A. N. O. N. said:
Hello friends

I need to write a shell utility. It will read the contents of a file,
perform a string replacement on those contents, and print out the result.
It will work similarly to grep except that it performs string replacement
in addition to matching, and it does not support regular expressions or
reading from standard input. But because of the similarities, our command
will be called strep (not to be confused with "strep throat").

Here is how the strep command works. Imagine a file named test.txt
contains the following lines:

foo a b c d foo e
A foo B
0 foo 1 2 foo

We could replace all occurrences of foo with goo as follows (assume we
are in the same directory containing foo.txt):

bash$ strep foo goo test.txt
goo a b c d goo e
A goo B
0 goo 1 2 goo
bash$

There are a couple of things that should be noted. First, the arguments
to strep are passed via the command line and can be accessed through the
variable argv in main. The user must enter three arguments after the
command. The first argument is the string to match (e.g., foo), the
second is the replacement string (e.g., goo), and the third is the file
to read from (e.g., test.txt).

The second thing to note with regard to the example above is that strep
replaces all occurrences of foo with goo on a particular line, not just
the first occurrence (although it will not replace a single string that
spans multiple lines).

Please send me full code for strep.c ASAP thanks.

PS this is not homework

There are very nice programming languages, for the manipulation of text.
This one has been superseded by Perl, but I like it because
it is a bit easier to learn. Using the fine manual, you can
investigate little bits of the language, and build up your
knowledge. I'm not really a Perl programmer, but what little
I've done, required reading a lot more of the two manuals
I purchased. This, by comparison, is free.

http://www.gnu.org/software/gawk/manual/gawk.pdf

Try page 45 of that PDF.

awk ’{ sub(/a+/, "<A>"); print }’ input.txt >output.txt

The example shows how the sub() function can make substitutions
in a text file. In this case, the program is so short, it is
one line, and can be typed into a command prompt, and used to
process a text file. In your example, perhaps you could do
something like this.

awk ’{ sub(/foo/, "goo"); print }’ input.txt >output.txt

The hardest part, would be getting a copy of the program
and setting it up.

http://gnuwin32.sourceforge.net/packages/gawk.htm

Since you can do useful things in one line, at least
some of the things you do, won't take a lot of time
and effort. You showed "bash" in your question and
my assumption is your system has some variant of that
program already. That Sourceforge link, is if you
wanted to try it in a Windows environment.

More info here...

http://en.wikipedia.org/wiki/Awk

Paul
 
S

Seebs

I need to write a shell utility.

Oddly, you then proceeded to ask us to write it for you, but!

No problem. We'll help you out. Anything for a friend.
It will read the contents of a file,
perform a string replacement on those contents, and print out the result.

That's great! Do you have any idea when it will do this?
It will work similarly to grep except that it performs string replacement
in addition to matching, and it does not support regular expressions or
reading from standard input. But because of the similarities, our command
will be called strep (not to be confused with "strep throat").

That's very clever.

Well, let's try our first pass. I'm afraid this won't be totally portable:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
int cmdlen, a1len, a2len;
char *cmdbuf;
char *a1buf;
char *a2buf;
int *arglens[] = { NULL, &a1len, &a2len };
char **argbufs[] = { NULL, &a1buf, &a2buf };
int i;

cmdlen = strlen("sed -e 's///g' ");
for (i = 1; i <= 3; ++i) {
cmdlen += strlen(argv);
if (i > 0 && (i*i) < 8) {
*arglens = strlen(argv);
}
}
for (i = 1; i <= 2; ++i) {
char *s = argv;
int *len = arglens;
while (*s) {
cmdlen += (*s == '.' || *s == '/' || *s == '\\');
*len += (*s == '.' || *s == '/' || *s == '\\');
++s;
}
}
for (i = 1; i <= 2; ++i) {
char *s = argv;
*argbufs = malloc(*arglens);
char *t = *argbufs;
while (*s) {
if (strchr("./\\", *s))
*t++ = '\\';
*t++ = *s++;
}
}
cmdbuf = malloc(cmdlen);
sprintf(cmdbuf, "sed -e 's/%s/%s/g' %s", a1buf, a2buf, argv[3]);
system(cmdbuf);
return 0;
}
Here is how the strep command works.

It seems almost as though -- and I'm going out on a limb here -- you're
giving us a description of what it does, rather than how it works.

How it works, I am afraid, may remain a mystery.
Imagine a file named test.txt
contains the following lines:
foo a b c d foo e
A foo B
0 foo 1 2 foo

How could Lennon have omitted this haunting refrain? Clearly, the song
is much improved. But I think we need more "goo" and less "foo"...
We could replace all occurrences of foo with goo as follows (assume we
are in the same directory containing foo.txt):

That would make it seem a lot more like the Beatles, yes. But I am
confused. What is "foo.txt"? You never mentioned it before or after.
Clearly, though, this is a key component of the functionality of this
program -- it must only work in a directory containing a file called
"foo.txt". I'll leave it for you to fix that.
bash$ strep foo goo test.txt
goo a b c d goo e
A goo B
0 goo 1 2 goo
bash$

Ahh, yes. And this is C, and you are posting, goo goo ... oh, nevermind.
There are a couple of things that should be noted. First, the arguments
to strep are passed via the command line and can be accessed through the
variable argv in main.

This is going to take some doing. Let me correct the first few lines of
my program:

@@ -3,7 +3,7 @@
#include <string.h>
#include <unistd.h>

-int main(int argc, char *argv[]) {
+int main(int argc, char *ARGUMENTS[]) {
int cmdlen, a1len, a2len;
char *cmdbuf;
char *a1buf;
@@ -11,6 +11,7 @@
int *arglens[] = { NULL, &a1len, &a2len };
char **argbufs[] = { NULL, &a1buf, &a2buf };
int i;
+ char **argv = ARGUMENTS;

cmdlen = strlen("sed -e 's///g' ");
for (i = 1; i <= 3; ++i) {

Good thing you specified that, I would have accessed them through the
*parameter* argv, but that wouldn't have complied with the ultimate spec!
Now it's correctly accessed through the variable argv.
The user must enter three arguments after the
command. The first argument is the string to match (e.g., foo), the
second is the replacement string (e.g., goo), and the third is the file
to read from (e.g., test.txt).

Got it. The program enforces this ruthlessly by crashing if the user
doesn't do that.
The second thing to note with regard to the example above is that strep
replaces all occurrences of foo with goo on a particular line, not just
the first occurrence (although it will not replace a single string that
spans multiple lines).

That's why we have a /g.
Please send me full code for strep.c ASAP thanks.

You have not provided an email address, so I'm afraid I can't.
PS this is not homework

Of course not. But if it were, I have every confidence that your professor
would accept this program. If he gives you crap about it, tell him it was
written by a former member of the ISO C committee and he'll be sure to award
full marks.

-s
 
K

Keith Thompson

A. N. O. N. said:
I need to write a shell utility. It will read the contents of a file,
perform a string replacement on those contents, and print out the result.
It will work similarly to grep except that it performs string replacement
in addition to matching, and it does not support regular expressions or
reading from standard input. But because of the similarities, our command
will be called strep (not to be confused with "strep throat").

Here is how the strep command works. [snip]
Please send me full code for strep.c ASAP thanks.

Suggested reading:
http://www.catb.org/~esr/faqs/smart-questions.html
particularly:
http://www.catb.org/~esr/faqs/smart-questions.html#bespecific
http://www.catb.org/~esr/faqs/smart-questions.html#urgent
PS this is not homework

Then why is it urgent? And if you want us to write the whole thing
for you, why do you need to be involved at all?
 
C

Chris McDonald

Of course not. But if it were, I have every confidence that your professor
would accept this program. If he gives you crap about it, tell him it was
written by a former member of the ISO C committee and he'll be sure to award
full marks.


Tell your professor it was written by someone who has never attended a
single CS class, and he'll accept is as your own work.

</in-the-voice-of-spinoza1111>

Sorry, just too obvious!
 
U

user923005

Barry wrote: [snip]
What is shell?

Found on the beach, left by small dead animals.

I'm pretty sure it's a Dutch energy and petrochemicals company.
Or maybe something filled with trinitrotoluene that detonates on
impact.
One or the other.
 
S

sfuerst

PS this is not homework

Goodo. I guess this might just almost work then:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int c,char**v)
{
if (c^4)exit(puts("strep before after file"));
{
char a[strlen(1[v])+strlen(2[v])+strlen(3[v])+c*c];
return system((sprintf(a,"sed s@%s@%s@g<%s",1[v],2[v],3[v]),a));
}
}

Of course if you do hand that in as homework, you may have a little
explaining to do. Unfortunately, it uses regular expressions, and
will mysteriously fail to correctly replace email addresses.

Steven

ps. Yes, some of the formatting is just a little evil...
 
U

user923005

My point was shell is ambiguous. Bourne, C, K, T etc...

He wasn't trying to 'bash' you. Sorry if that's a little korny.
Since this is to be a C program, clearly the O.P. wants csh.
But nicely topical, because both shell gas stations and shell scripts
use pipes.
 
B

Beej Jorgensen

sfuerst said:
ps. Yes, some of the formatting is just a little evil...

Allow me to fix that:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int c,char**v){if(c^4)exit(puts("strep before after file"));
{char a[strlen(1[v])+strlen(2[v])+strlen(3[v])+c*c];return
system((sprintf(a,"sed s@%s@%s@g<%s",1[v],2[v],3[v]),a));}}

Now the formatting is correctly evil. ;-)

-Beej
 
U

user923005

sfuerst   said:
ps.  Yes, some of the formatting is just a little evil...

Allow me to fix that:

  #include<stdio.h>
  #include<stdlib.h>
  #include<string.h>
  int main(int c,char**v){if(c^4)exit(puts("strep before after file"));
  {char a[strlen(1[v])+strlen(2[v])+strlen(3[v])+c*c];return
  system((sprintf(a,"sed s@%s@%s@g<%s",1[v],2[v],3[v]),a));}}

Now the formatting is correctly evil. ;-)

Or even:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int q0,char**q1)??<if(q0??'4)exit(puts("\163\164\162\145"
"\160\40\142\145\146\157\162\145\40\141\146\164\145\162\40\146"
"\151\154\145"));??<char q2??(strlen(1??(q1??))+strlen(2??(q1??))+
strlen(3??(q1??))+q0*q0??);return system((sprintf(q2,
"\163\145\144\40\163\100\45\163\100\45\163\100\147\74\45\163",
1??(q1??),2??(q1??),3??(q1??)),q2));??>??>

Note: requires c99 compiler due to variable length array.
 
D

Dik T. Winter

> I need to write a shell utility. ....
> Please send me full code for strep.c ASAP thanks.

Why do you call a shell utility strep.c? But here:

#!/bin/sh
sed -e "s/$1/$2/g" <$3
 
A

Antoninus Twink

I need to write a shell utility. It will read the contents of a file,
perform a string replacement on those contents, and print out the result.

I see that no one has given you a serious answer yet, so here's some
code to get you started. It has the advantage that it doesn't use any
dynamic memory. An interesting question is what to do in the boundary
case when the string to be matched is the empty string.

#include <stdio.h>
#include <stddef.h>

int main(int argc, char **argv)
{
FILE *fp = NULL;
int rv = 0;
if(argc >= 4 && (fp = fopen(argv[3], "r"))) {
char *m = argv[1]; /* next position to match */
int c;
while((c = fgetc(fp)) != EOF) {
if(*m == c) {
if(*++m == '\0') {
fputs(argv[2], stdout);
m = argv[1];
}
} else {
char *s;
for(s = argv[1]; s < m; s++)
putchar(*s);
putchar(c);
}
}
} else {
fprintf(stderr, "Usage: %s STRING REPLACEMENT FILE\n", *argv);
rv = 1;
}
if(fp)
fclose(fp);
return rv;
}
 
S

Seebs

I see that no one has given you a serious answer yet, so here's some
code to get you started.
Heh.

while((c = fgetc(fp)) != EOF) {
if(*m == c) {
if(*++m == '\0') {
fputs(argv[2], stdout);
m = argv[1];
}
} else {
char *s;
for(s = argv[1]; s < m; s++)
putchar(*s);
putchar(c);
}
}

I was going to chew you out for giving the guy a homework answer
until I spotted the trap. Nicely done! By the time he figures out
why this doesn't work, he might as well have just written it himself.
That's arguably better than my fancy example which works fine,
but which no one would ever accept as an answer.

-s
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top