Nikon D5000 | Web Development jobs | Find jobs | Breast Enlargement | Internet Dating
how to edit files on the command line? [Archive] - Muslim Programmers & Designers Community - Islamic Webhosting and Nasheeds

PDA

View Full Version : how to edit files on the command line?



tawbah7
06-17-2003, 04:20 AM
salaams,

i'm trying to write a post installation bash shell script to perfom all the dirty work of setting up my linux box from scratch.

basically it is supposed to create users, install packages, edit configuration files and start services etc.

one of the things i need to be able to do is edit config files on the fly, that is, let the shell script take care of editing with no human interaction.

i undestand that sed and awk can be used with pattern matching and substitution capabilities to extract certain text from files and reorganize it but as far as i know...the changes do not take effect to the original file

for example, let us say that post-installation i would like a shell script to automatically open the httpd.conf file and enable all user directories (by default the user dirs are disabled). i know how to use the sed regular expression syntax to manipulate data from input files but how do i save changes to the original files?

ok...well i thought of one way to do this but it doesn't seem like a good aproach

here's what i did:

first, this is my example file:

# cat test
# first:second
# one:two

then i did this:

# TEST=`sed 's/first/FIRST/' test
# printf "$TEST\n" > test

and now this is what the modified version of test looks like

# cat test
# FIRST:second
# one:two

so what i did above was tried to capitalze the word 'first' in the file called test, i saved the output of the sed command in the TEST shell variable and rerwote it back in to the test file...i had to append an ending '\n' character though since it was not present in the TEST shell variable, though it should have been.

is there even a better way of doing this altogether?

tawbah7
06-17-2003, 03:41 PM
i got an answer from the sys admin at my uni if anyone is interested....

This is dangerous, if the size of the test file gets very large, it
will not fit into a shell variable.

Simple editing in place can be done via ed (there was an example in
the notes).

If you're essentially scanning the file looking for a pattern to
change, Perl is better, because it has some features we did not
discuss, namely the -i, -e and -p flags (see 'man perlrun' for a
description).

-p makes a loop around your perl code (specified via -e), and -i makes
it work in-place on the target file. So

perl -p -i -e 's/first/FIRST/' test

will apply the subs to all lines of test and rewrite in place. If you
want a backup kept (a good idea), do

perl -p -i.bak -e 's/first/FIRST/' test

Of course you need to make sure your regex's only target the right
pieces! It can be hard to make sure the context is tight enough.

Hope this helps.

John

baba
06-17-2003, 11:24 PM
Have you tried 'sed_inplace'? I came across it recently (since it's used for the Postfix port) in FreeBSD, it's in the ports collection under 'textproc'.

tawbah7
06-20-2003, 01:57 PM
Originally posted by baba
... it's in the ports collection under 'textproc'.

come again?

well i did a man on sed in both red hat and solaris and under "see also", it did not show sed_inplace but it does refer to ed and perl

for red hat it does the trick very nicely
for example, i did this to enable user directories in the apache config file:

perl -p -i.bak -e 's/UserDir disable/UserDir enable all/' /etc/httpd/conf/httpd.conf

something esle that i found pretty neat... connecting to ftp via a script....this would be useful if after installation there are still aditional packages that need to be downloaded

#!/bin/bash

ftp -n << EOF
open ftp.host.com
user username password
get /pub/downloads/.../php-mysql-...i386.rpm
close
exit
EOF

baba
07-02-2003, 07:27 PM
I forgot that you were a Linux user. I used FreeBSD mostly.

So I have sed_inplace and you don't?

*grins*

baba
07-14-2003, 10:46 PM
Hang on, surely you can get sed_inplace on Linux? It can't be FreeBSD specific...