Edit a file via bash script

Solution 1:

sed '/enabled=1/a\priority=10' /etc/yum.repos.d/epel.repo

Specify an in-place editing option (-i) if you want (makes the backup with .bak extension first):

sed -i.bak '/enabled=1/a\priority=10' /etc/yum.repos.d/epel.repo

Now i want use sed to do the following 1.Find the FIRST occurrence of worker_processes in that conf and replace text with worker_processes 4;

sed '0,/worker_processes [0-9]*;/s//worker_processes 4;/' /etc/nginx/nginx.conf

One last suggestion I used sed -i 's/enabled=0/enabled=1/g' /etc/yum.repos.d/remi.repo to change enabled=0 to enabled=1 under the [remi] section in remi.repo. However i have a feeling that it may modify all enabled=0 in that file , which will wreak the server.Can anyone suggest a better code

Here for you:

sed '/\[remi\]/,/enabled=0/ { s/enabled=0/enabled=1/ }' remi.repo

Another stuff i am not sure of:P I want to edit a file that has this as Text Testing = "0"(Yes it has quotes and i need to keep it) It should be modified from Testing = "0" to Testing = "1"(with quotes)

sed 's/Testing = "0"/Testing = "1"/g'

Also i need to add some text with quotes at the end of a file with sed Like "Thanks Quanta"(with quote) For php you put a \ with echoing quotes , don't know how it is done for bash

sed '$a"Werulz, you are welcome"\'

Another thing I need to modify a line in a conf but i don't remember what is the whole of text to replaced

Like its listen = something; , i want to modify it to listen = /tmp/php5-fpm.sock;

sed 's/listen = .*/listen = \/tmp\/php5-fpm.sock;/'

Solution 2:

Like ptman said, Augeas can save you from using sed/awk:

$ augtool -s set /files/etc/yum.repos.d/epel.repo/epel/priority 10

will set the priority of the epel repository to 10. If the priority key already exists, it will set its value, otherwise it will add a priority entry after the last entry in the section.

The same goes for other values you may want to modify.

If you want to modify several values, you can even use augtool as an interpreter:

$ cat epel.augtool
#!/usr/bin/augtool -sf

# Make a variable
defvar epel /files/etc/yum.repos.d/epel.repo/epel

# Set values
set $epel/enable 1
set $epel/priority 10
set $epel/Testing 0
$ chmod +x epel.augtool
$ ./epel.augtool
Saved 1 file(s)

If you want to keep it in your bash script, you can pipe the commands into augtool:

cat <<EOF | augtool
# Make a variable
defvar epel /files/etc/yum.repos.d/epel.repo/epel

# Set values
set $epel/enable 1
set $epel/priority 10
set $epel/Testing 0
save
EOF

See man augtool for more options.

There are several avantages to using Augeas vs sed/awk:

  • Augeas has parsers that know about the file syntax (in this case, yum.repo syntax). You won't take the risk of breaking the file syntax if your regex are wrong;
  • the Augeas yum.repo parser knows about yum.repo sections, so you know that you're editing the right section;
  • Augeas is idempotent. It will only make changes to the file if there are changes to be made. If all the values are already set, it won't modify the file;
  • Augeas has a Puppet provider, so you can easily automate this in a Puppet infrastructure;
  • if parameters don't exist yet, Augeas will create them for you. If the section doesn't exist yet, Augeas will also create it, with the proper syntax.

Solution 3:

You might also want to check out Augeas, tool for programmatically modifying config files. It is very powerful in combination with Puppet (a configuration management system), but can also be used by itself.