Re: URGENT fast answer needed
- From: Paul <nospam@xxxxxxxxxx>
- Date: Mon, 26 Oct 2009 19:10:08 -0400
A. N. O. N. wrote:
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
.
- References:
- URGENT fast answer needed
- From: A. N. O. N.
- URGENT fast answer needed
- Prev by Date: Re: URGENT fast answer needed
- Next by Date: Re: URGENT fast answer needed
- Previous by thread: Re: URGENT fast answer needed
- Next by thread: Re: URGENT fast answer needed
- Index(es):
Relevant Pages
|