I am trying to write a sed command to do the following.
I have a line in my file, something like this
*.info;mail.none;authpriv.none;cron.none /var/log/messages
This has to be replaced with,
*.info;mail.none;authpriv.none;cron.none;local2.none;local5.none /var/log/messages
Conditions
- There is no fixed spacing between cron.none and /var/log/messages, the spacing might vary from file to file. original *.info;mail.none;authpriv.none;cron.none /var/log/messages desired *.info;mail.none;authpriv.none;cron.none;local2.none;local5.none /var/log/messages
or
original
*.info;mail.none;authpriv.none;cron.none /var/log/messages
desired
*.info;mail.none;authpriv.none;cron.none;local2.none;local5.none /var/log/messages
just append ;local2.none;local5.none
- Sometime the file might have already ;local2.none;local5.none, in this case ignore the append. ex
*.info;mail.none;authpriv.none;cron.none;local2.none;local5.none /var/log/messages
*.info;mail.none;authpriv.none;cron.none;local2.none;local5.none /var/log/messages
- sometimes the file might have local6.none, but no local2.none;local5.none, in this case append ;local2.none;local5 ex
*.info;mail.none;authpriv.none;cron.none;local6.none /var/log/messages
*.info;mail.none;authpriv.none;cron.none;local2.none;local5.none;local6.none /var/log/messages
Below are the few things I tried.
1)
sed -i -e "/*.info;mail.none;authpriv.none;cron.none;local2.none;local5.none.*\ \/var\/log\/messages/! /*.info;mail.none;authpriv.none;cron.none.*\ \/var\/log\/messages/ s/cron.none/cron.none;local2.none;local5.none/g" $SYSLOGCONFFILE
sed -i -e "/*.info;mail.none;authpriv.none;cron.none.*\ \/var\/log\/messages/.*local2.none;local5.none.*/!s/cron.none/cron.none;local2.none;local5.none/g" $SYSLOGCONFFILE
sed -i -e "/*.info;mail.none;authpriv.none;cron.none;local2.none;local5.none.*\ \/var\/log\/messages/! /*.info;mail.none;authpriv.none;cron.none.*\ \/var\/log\/messages/ s/cron.none/cron.none;local2.none;local5.none/g" $SYSLOGCONFFILE
nothing seems working. TIA
CodePudding user response:
awk is a better tool for accomplishing what you are trying to do, in this case:
#!/bin/sh
awk -v inject=";local2.none;local5.none" -v postfix=";local6.none" '{
exist=index( $1, inject ) ;
other=index( $1, postfix ) ;
print "\n\texist=", exist ;
print "\n\tother=", other ;
if( exist > 0 ){
print "\n *1*"
print $0 ;
}else{
print "\n *2*"
if( other > 0 ){
beg=substr( $1, 1, other-1 ) ;
end=substr( $1, other ) ;
printf("%s%s%s %s\n", beg, inject, end, $2 ) ;
}else{
printf("%s%s %s\n", $1, inject, $2 ) ;
} ;
} ;
}'
CodePudding user response:
Using sed
$ sed -i.bak '/cron.none.*local2.none;local5.none/!{s/cron.none/&;local2.none;local5.none/}' input_filr
*.info;mail.none;authpriv.none;cron.none;local2.none;local5.none /var/log/messages
*.info;mail.none;authpriv.none;cron.none;local2.none;local5.none /var/log/messages
*.info;mail.none;authpriv.none;cron.none;local2.none;local5.none;local6.none /var/log/messages
*.info;mail.none;authpriv.none;cron.none;local2.none;local5.none /var/log/messages