Need help in correcting sed script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in correcting sed script
# 1  
Old 02-14-2014
Need help in correcting sed script

Hi All,

I have a big configuration log where I want to change string "aaa" to "bbb" under
auto-config-policy only right after "deny", "accept" and "hold" strings, nowhere else.



<<<< text >>>>>
Code:
auto-config-policy test
     deny aaa precedence 4 profile aaa any  
     deny aaa precedence 6 domain aaa
     accept aaa precedence 4 profile aaa any
     hold aaa precedence 4 profile aaa any

auto-upgrade-policy  test2
     upgrade aaa precedence 4 profile default-aaa any  
     upgrade aaa precedence 6 domain default vlan 345 
     upgrade aaa precedence 4 profile default-aaa any

<<<<<text>>>>

To do that I have used the following sed script.


Code:
#!/bin/sh

OLD_STRING1="deny aaa precedence"                                                                               
NEW_STRING1="deny bbb precedence"                                                                               
OLD_STRING2="accept aaa precedence"                                                                               
NEW_STRING2="accept bbb precedence"   
OLD_STRING3="hold aaa precedence"                                                                               
NEW_STRING3="hold bbb precedence"                                                                                                                                                         
CONFIG_FILE=/tmp/conf.log

fix()                                                                   
{

  if [ ! -e $CONFIG_FILE ] ; then
     echo "file not present"
     return 1
  fi 
                       
  if [ -r $CONFIG_FILE ] ; then   
    sed <$CONFIG_FILE -e "s/$OLD_STRING1/$NEW_STRING1/g" -e "s/$OLD_STRING2/$NEW_STRING2/g" -e "s/$OLD_STRING3/$NEW_STRING3/g" > /tmp/a-$$
    mv /tmp/a-$$ $CONFIG_FILE
  fi 
}
 
fix

Can any body help me in correcting/updating the script. Only shell scripting needs to be used for this purpose.


Thanks.............

Last edited by Scrutinizer; 02-14-2014 at 05:43 PM.. Reason: code tags also for data
# 2  
Old 02-14-2014
You can accomplish the task with a single sed command:

Code:
sed -i.BKUP '/^auto-config-policy/,/^hold/s/aaa/bbb/' conf.log

cat conf.log
auto-config-policy test
deny bbb precedence 4 profile aaa any
deny bbb precedence 6 domain aaa
accept bbb precedence 4 profile aaa any
hold bbb precedence 4 profile aaa any

auto-upgrade-policy test2
upgrade aaa precedence 4 profile default-aaa any
upgrade aaa precedence 6 domain default vlan 345
upgrade aaa precedence 4 profile default-aaa any

cat conf.log.BKUP
auto-config-policy test
deny aaa precedence 4 profile aaa any
deny aaa precedence 6 domain aaa
accept aaa precedence 4 profile aaa any
hold aaa precedence 4 profile aaa any

auto-upgrade-policy test2
upgrade aaa precedence 4 profile default-aaa any
upgrade aaa precedence 6 domain default vlan 345
upgrade aaa precedence 4 profile default-aaa any

# 3  
Old 02-14-2014
Hi,

Thanks for helping me. But unfortunately your sed command is replacing "aaa" on other places also. see in red colors. I want to change "aaa" string only between "auto-config-policy test"
and "auto-upgrade-policy test2" . That to between deny/accept/hold and "precedence" (see in orange color) nowhere else.

<<<< text >>>>>
Code:
auto-config-policy test
      deny aaa precedence 4 profile aaa any 
      deny aaa precedence 6 domain aaa
      accept aaa precedence 4 profile aaa any
      hold aaa precedence 4 profile aaa any

auto-upgrade-policy test2
      upgrade aaa precedence 4 profile default-aaa any 
      upgrade aaa precedence 6 domain default vlan 345 
      upgrade aaa precedence 4 profile default-aaa any

upgrade-profile aaa default-aaa
      autoupgrade configuration
      autoupgrade firmware

aaa  00:11:AA-DD-CC-AA
        use upgrade default-aaa
        use config   defautl-aaa

<<<<<text>>>>


Thanks.............

Last edited by Scrutinizer; 02-14-2014 at 05:43 PM.. Reason: code tags also for data
# 4  
Old 02-14-2014
Code:
sed -i.BKUP '/^auto-config-policy/,/^auto-upgrade-policy/ s/ aaa precedence/ bbb precedence/' conf.log

# 5  
Old 02-14-2014
@temp.sha: What wasn't working with your original script in post #1?

--edit-- Did you mean to simplify your script? Can it be awk also? like e.g.:
Code:
awk '$1~/^(deny|accept|hold)$/ && $2==a {$2=b}1' a=aaa b=bbb "$CONFIG_FILE"


Last edited by Scrutinizer; 02-14-2014 at 06:02 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Correcting awk error

the following code turns back ticks "`" to new lines "\n". Then, it attempts to grab only a certain section of the output and excludes lines that contain particular patterns...i.e. "ZooLine|echo|opencert". awk -vs1="\`" '{gsub(s1,"\n",$0)} 1 {print ; if(/NewLine \(\)/,/}/{if(!/NewLine|echo... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Help correcting file with differing number of fields

Hi all, I have a tab separated file, and one of the fields is sub-delimited by colon. The problem is there can be zero to 4 colons within this field. When I try to change colons to tabs the result is a file with a differing number of fields. I want to go from: a:b:c:d:e a:b:c a:b:c:d:e a... (4 Replies)
Discussion started by: torchij
4 Replies

3. Shell Programming and Scripting

need help in correcting the code

Hi Guys ! can anyone help me to write the code doing same thing without using awk. is it possible using cut command? awk '{c++} END {for(k in c} print k "\t\t" c;}' file_name | sort -nrk 2 | column -t thanks in advance BR Ewa (4 Replies)
Discussion started by: me_newbie
4 Replies

4. UNIX Desktop Questions & Answers

Correcting the display resolution

I have an issue with the display resolution under Xfce. The monitor can handle 1024x768, but the screen display is 800x600. The control panel does not allow me to change this. I also found the file .config/xfce4/xfconf/xfce-perchannel-xml/displays.xml in which I have made changes, but after a... (0 Replies)
Discussion started by: figaro
0 Replies

5. AIX

Help me in correcting sendmail.cf

Hi all, In AIX I need to set the below two as of now these are not. 1)Opnoexpn is not set in sendmail.cf 2)Opnovrfy is not set in sendmail.cf Please let me know the steps to set Opnoexpn and Opnovrfy in sendmail.cf Thanks and Regards, Pavankumar (0 Replies)
Discussion started by: pavankumar432
0 Replies

6. UNIX for Dummies Questions & Answers

Correcting the time on FreeBSD

Possibly this is not even a FreeBSD issue, but a BIOS issue. Upon installation of FreeBSD, the time is set using the standard feature of selecting a time zone. Some installations are correctly set to the current time, but others are either one or two hours off. So the time is read from the time... (0 Replies)
Discussion started by: figaro
0 Replies

7. Shell Programming and Scripting

sed script

how to convert the follow sed script file into a command line ? example: /^\.TS/,/^\.TE/{ /^$/p } I have tried the below but it is not working: # sed -n "/^\.TS/,/^\.TE/{/^$/p}" file file: 111 .TS 222 $333 << extract this line 444 .TE 555 (2 Replies)
Discussion started by: 3Gmobile
2 Replies

8. Solaris

Correcting system time

I've installed Solaris 8, but didn't quite give it the correct (machine) time. Think the machine is out of sync. Can I change that afterwards or do I have to do another install right from the beginning? (1 Reply)
Discussion started by: kuultak
1 Replies

9. Shell Programming and Scripting

Correcting script

Hi! I have for example a script/file: +1 echo "toto" +2 echo "abcdef +3 echo "dqmsl" $r "dsqlfj" +4 cat titi | tr \t' ';' +5 exit --------------------------------------- I try to find a solution who say: <ERROR> The character " has been forgotten a the line 2 The character... (7 Replies)
Discussion started by: Castelior
7 Replies
Login or Register to Ask a Question

Featured Tech Videos