Get multi-line sed not to run if regex is found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get multi-line sed not to run if regex is found
# 1  
Old 07-28-2016
Get multi-line sed not to run if regex is found

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:

Code:
{G;/#/! s/\$/\t_mac_address\t${ARPMAC}/;}
{/#/!;G;s/\$/\t_mac_address\t${ARPMAC}/;}
{G;/#/!s/\$/\t_mac_address\t${ARPMAC}/;}

None of those gave me the desired output.

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.

Thanks!
# 2  
Old 07-28-2016
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
        }

This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-28-2016
Your solution works perfectly, and is far easier to read than my own, thank you very much!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multi line regex for search and replace

I have text file like below: a.txt Server=abc Run=1 Time=120.123 Tables=10 Sessions=16 Time=380.123 Version=1.1 Jobs=5 Server=abc Run=2 Time=160.123 Tables=15 Sessions=16 Time=400.258 Version=2.0 (1 Reply)
Discussion started by: sol_nov
1 Replies

2. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

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)
Discussion started by: satishmallidi
12 Replies

3. Shell Programming and Scripting

sed multiple multi line blocks of text containing pattern

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)
Discussion started by: andyatit
3 Replies

4. Shell Programming and Scripting

sed to replace a line with multi lines from a var

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)
Discussion started by: bblondin
4 Replies

5. Shell Programming and Scripting

SED - insert space at the beginning of line and multi replace command

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)
Discussion started by: piynik
5 Replies

6. Shell Programming and Scripting

Multi-line filtering based on multi-line pattern in a file

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)
Discussion started by: Finja
2 Replies

7. Shell Programming and Scripting

Using Sed to remove part of line with regex

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)
Discussion started by: msarro
4 Replies

8. Shell Programming and Scripting

sed - multi-line regex syntax eval puzzle

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)
Discussion started by: gosleddog
6 Replies

9. Shell Programming and Scripting

perl regex multi line cut

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)
Discussion started by: tip78
9 Replies

10. Shell Programming and Scripting

sed: delete regex line and next line if blank

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)
Discussion started by: one71
2 Replies
Login or Register to Ask a Question