Register
It is currently Tue Nov 25, 2014 3:52 pm

Replace text in some file


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Sat Feb 28, 2009 4:31 am   

Joined: Sat Feb 28, 2009 4:17 am
Posts: 2
Lets say that i have file rc.conf.
Part of this file look like this:
Quote:
# -----------------------------------------------------------------------
# DAEMONS
# -----------------------------------------------------------------------
#
# Daemons to start at boot-up (in this order)
# - prefix a daemon with a ! to disable it
# - prefix a daemon with a @ to start it up in the background
#
DAEMONS=(syslog-ng hal @cpufreq !network netfs !dhcdbd !networkmanager wicd @alsa fam @cups @laptop-mode @mysqld gdm)


1. thing that i want to do is to search if some deamon exist and if not add it to this line. I managed this with

Quote:
demon=`cat /etc/rc.conf | grep "DAEMONS=(" | tr -d "DAEMONS=(" | tr -d ")"`
if [[ "$demon" != *alsa* ]]
then
AREAD=$(cat /etc/rc.conf | grep "DAEMONS=(" | tr -d "DAEMONS=(" | tr -d ")" )
AWRITE="$demon @alsa"
sed -i s/"$AREAD"/"$AWRITE"/g /etc/rc.conf
fi


2. thing that i want to do is to find out if alsa exist and if it does to rename it to @alsa
Any clues?


Top
 Profile WWW  
 PostPosted: Mon Mar 02, 2009 3:15 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 569
Code:
sed '^DAEMONS/{/ alsa/s// @alsa/; / any.d/!s)$/ any.d)/}' rc.conf
This means:
in rc.conf,
if a line begins with "DAEMONS",
if this line contains " alsa", change it to " @alsa"
if this line does not contain "any.d", then change the ending ")" to "any.d)".
To apply changes in place, add the -i option.


Top
 Profile  
 PostPosted: Mon Mar 02, 2009 5:41 am   

Joined: Sat Feb 28, 2009 4:17 am
Posts: 2
Thx.

How about command if alsa doesnt exist to add it ?


Top
 Profile WWW  
 PostPosted: Mon Mar 02, 2009 6:13 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Well I can't say this will work at all, this is just a pointer on how you can do it. This is mostly because I'm at work and I don't have any ArchLinux install close at hand. I don't have any unix/linux machine close for that matter.

Code:
#!/bin/bash
# Set the pattern you want to search for. Usage: ./script.sh alsa
pattern=$1

# Create the list of the current Daemons variable
current_list=$(cat /etc/rc.conf | grep DAEMONS | cut -d'\(' -f2 | cut -d'\) -f1)
# Loop throu the list
for i in $current_list; do
   # remove the first character if it's an @ or a !. This is because pattern matching doesn't work with them there.
   current=$(echo $i | sed -e "s/^[@!]//")
   # You could use if-else-statement here, but I like case and it likes regexps.
   case $i in
      case "$pattern")
         case $i in
            case ^\@)
               extra="\@$current"
            ;;
            case ^\!)
               extra="\!$current"
            ;;     
            case *)
               extra=$current
            ;;
      esac
      ;;
      case *)
         daemon_list="$daemon_list $i"
      ;;
   esac
done
echo "DAEMONS=\($daemon_list $extra\)"


As I said above, highly untested ;) this will just output the DAEMON=() line so no worries about the rc.conf.
Recommended for a later date is just to sed the line like this
Code:
new_daemon="DAEMONS=\($daemon_list $extra\)"
cp /etc/rc.conf /etc/rc.conf.org
sed -i "s/$(cat /etc/rc.conf | grep DAEMONS)/$new_daemon/" /etc/rc.conf


Best regards
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP