Hello, everyone. Thanks for taking the time to read my post.
I have nagios config files for which I'm adding the custom variable _mac_address. I have a sed script that places this variable into an existing file. The problem I'm having is if a line in the file is commented out, I don't want the sed script to run on that line. My thought was some usage of /#/! should tell sed not to function if # is included in a line, but that hasn't worked. Here is my existing mostly functional sed which is nestled inside a bash script so I had to escape things like $ when it meant end of line as opposed to a bash variable:
Code:
sed -i -n "
:a
/address.*${RADIO}\$/! {
N
ba
}
{G;s/\$/\t_mac_address\t${ARPMAC}/;}
p
s/^.*\$//
:b
\$! {
N
bb
}
s/\n//
p
" ${NAGIOSFILE}
The three shell variables in the sed script are ${RADIO} which is an IP address, ${BIGMAC} which is the mac address of the IP in ${RADIO}, and ${NAGIOSFILE} which is the file containing the text that sed needs to edit.
If I had this block of text before running this sed script:
Code:
define host{
use generic-host
host_name Hostname
alias device of some sort
address 192.168.10.140
parents switch
hostgroups radios
}
This is what it would look like after it:
Code:
define host{
use generic-host
host_name Hostname
alias device of some sort
address 192.168.10.140
_mac_address aa:bb:cc:11:22:33
parents switch
hostgroups radios
}
The problem happens if I have this set of text initially:
Code:
#define host{
# use generic-host
# host_name Hostname before
# alias old device of some sort
# address 192.168.10.140
# parents switch
# hostgroups radios
# }
define host{
use generic-host
host_name Hostname
alias device of some sort
address 192.168.10.140
parents switch
hostgroups radios
}
What I'm looking to have is this:
Code:
#define host{
# use generic-host
# host_name Hostname before
# alias old device of some sort
# address 192.168.10.140
# parents switch
# hostgroups radios
# }
define host{
use generic-host
host_name Hostname
alias device of some sort
address 192.168.10.140
_mac_address aa:bb:cc:11:22:33
parents switch
hostgroups radios
}
But what I'm getting is:
Code:
#define host{
# use generic-host
# host_name Hostname before
# alias old device of some sort
# address 192.168.10.140
_mac_address aa:bb:cc:11:22:33
# parents switch
# hostgroups radios
# }
define host{
use generic-host
host_name Hostname
alias device of some sort
address 192.168.10.140
parents switch
hostgroups radios
}
I've tried making changes to the substitution line in sed, such as:
How can I get the output I'm searching for from my sed script? I'm willing to go to awk if needed, but I'd like to stick with sed and would rather stay away from perl and heavier tools.
Not sure if I correctly understood your problem and its intricacies, but there seems to be quite simple a solution:
Code:
sed "/^#/{p;d}; /address.*${RADIO}\$/ s/\$/\n\t_mac_address\t${ARPMAC}/ " file
#define host{
# use generic-host
# host_name Hostname before
# alias old device of some sort
# address 192.168.10.140
# parents switch
# hostgroups radios
# }
define host{
use generic-host
host_name Hostname
alias device of some sort
address 192.168.10.140
_mac_address aa:bb:cc:11:22:33
parents switch
hostgroups radios
}
Hi,
I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :-
./sample.sh: line... (12 Replies)
Hi,
I have a log file which has sessionids in it, each block in the log starts with a date entry, a block may be a single line or multiple lines. I need to sed (or awk) out the lines/blocks with that start with a date and include the session id.
The files are large at several Gb.
My... (3 Replies)
I am trying to find a line in a file ("Replace_Flag") and replace it with a variable which hold a multi lined file.
myVar=`cat myfile`
sed -e 's/Replace_Flag/'$myVar'/' /pathto/test.file
myfile:
cat
dog
boy
girl
mouse
house
test.file:
football
hockey
Replace_Flag
baseball
... (4 Replies)
hi I am trying to use SED to replace the line matching a pattern using the command
sed 'pattern c\
new line
' <file1 >file 2
I got two questions
1. how do I insert a blank space at the beginning of new line?
2. how do I use this command to execute multiple command using the -e... (5 Replies)
I have a file with data records separated by multiple equals signs, as below.
==========
RECORD 1
==========
RECORD 2
DATA LINE
==========
RECORD 3
==========
RECORD 4
DATA LINE
==========
RECORD 5
DATA LINE
==========
I need to filter out all data from this file where the... (2 Replies)
Greetings everyone. Right now I am working on a script to be used during automated deployment of servers. What I have to do is remove localhost.localdomain and localhost6.localdomain6 from the /etc/hosts file. Simple, right? Except most of the examples I've found using sed want to delete the entire... (4 Replies)
sed novice bashing away at this....
I am trying to build a sed script that will find the instances of "cn" that have more than one "DirXML" value on them.... see sample below:
I am not having any luck with any variation that tries to find "DirXML.*\nDirXML.*". Isn't there a way to get sed to... (6 Replies)
hello mighty all
there's a file with lots of comments.. some of them looks like:
=comment
blabla
blablabla
bla
=cut
i'm trying to cut this out completely with this code:
$line=~s/^=.+?=cut//sg;
but no luck
also tryed to change it abit but still I don't understand how the... (9 Replies)
Hi,
I want to write a sed script which from
batiato:
batiato/giubbe:
pip_b.2.txt
pip_b.3.txt
pip_b.3mmm.txt
bennato:
bennato/peterpan:
123.txt
consoli:
pip_a.12.txt
daniele: (2 Replies)