Click to See Complete Forum and Search --> : A way to do text processing


soleblazer
08-25-2004, 09:48 AM
Hi all.

I would like to do something which is probably easy, but I am having difficulty.

I have a post install script that my kickstart server will perform. It basically disables services, copies files like motd, forces interfaces to 100/Full, etc.

The part I am having a problem with is on how to change text files. An example would be editing syslog.conf to add a few lines, etc.

How would be the best way to do this? Maybe someone could show a way to in a script snipet to add a line into a file. I think sed may be able to do it but I have had a bear of a time to get it working correctly. I think I could do this easily in perl but I have already written most of this monster in bash.

Tks for any pointers.

mrBen
08-25-2004, 10:11 AM
If you are simply adding text, then just do something along the lines of:


echo "Some Text" >> syslog.conf


If you are looking to insert text in the middle of the file, then things could be a little trickier, but not impossible, and you would probably be looking to use sed.

soleblazer
08-25-2004, 10:34 AM
Originally posted by mrBen
If you are simply adding text, then just do something along the lines of:


echo "Some Text" >> syslog.conf


If you are looking to insert text in the middle of the file, then things could be a little trickier, but not impossible, and you would probably be looking to use sed.

Yes, I should have stated that. I would need to do it in the middle of the file, and change a couple lines....can sed open a file, change a line and then save it?

mrBen
08-25-2004, 11:06 AM
Originally posted by soleblazer
Yes, I should have stated that. I would need to do it in the middle of the file, and change a couple lines....can sed open a file, change a line and then save it?

sed is a stream editor - it doesn't really handle files at all, only streams of data. (Having said that, you can get it to load a file into the stream)

If you are wanting to change something in the file, then you can use the substitute argument:


sed 's/replace this/with this/' myfile.txt


However, this will only deal with the first instance of a match. To do it globally, add a g after the last /. This will substitute all occurances of the text.

You can also use regexs, similar to Perl, to find the text you want.


Once you've checked to see that it works, you will need to pipe the result into the file. However, IIRC you will need to use a temporary file, as it will get confused if you try and pipe it into a file that is already being used.

bwkaz
08-25-2004, 07:04 PM
This is COMPLETELY NON PORTABLE, but if you use GNU sed version 4 or higher, you can give it the -i option to do "in-place editing". sed itself will create a temp file, do the substitutions (or whatever else), and then move the temp file back overtop of the original.

GNU sed versions before 4 don't have a -i option, and neither does any sed that isn't written by GNU (standard POSIX sed, if there is such a thing, doesn't support it).