swapping lines that match a condition using sed, perl or the like

I'm a bit new to regex and sed/perl stuff, so I would like to ask for some advice. I have tried several variations of scripts I've found on the net, but can't seem to get them to work out just right.

I have a file with the following information...

# Host 1
host 45583 {
    filename "junk1.cm";
    hardware ethernet 11:42:a3:d4:55:83;
    fixed-address 10.100.34.114;
    }
# Host 2
host D78C3 {
    filename "junk5.cm";
    fixed-address 10.100.34.117;
    hardware ethernet 11:42:a3:FD:78:C3;
    }
# Host 3
host 3A684 {
    filename "junk6.cm";
    fixed-address 10.100.34.119;
    hardware ethernet 11:42:a3:13:a6:84;
    }
# Host 4
host 46d54 {
    filename "junk4.cm";
    fixed-address 10.100.34.120;
    hardware ethernet 23:10:3d:14:6d:54;
    }

I have another script that is rearranging this just fine with one exception. The lines that have 'fixed-address' as the last line before the close bracket are working fine. The lines that have 'hardware ethernet' on them are causing my big script to misinterpret the information.

My desired result would be the following...

# Host 1
host 45583 {
    filename "junk1.cm";
    hardware ethernet 11:42:a3:d4:55:83;
    fixed-address 10.100.34.114;
    }
# Host 2
host D78C3 {
    filename "junk5.cm";
    hardware ethernet 11:42:a3:FD:78:C3;
    fixed-address 10.100.34.117;
    }
# Host 3
host 3A684 {
    filename "junk6.cm";
    hardware ethernet 11:42:a3:13:a6:84;
    fixed-address 10.100.34.119;
    }
# Host 4
host 46d54 {
    filename "junk4.cm";
    hardware ethernet 23:10:3d:14:6d:54;
    fixed-address 10.100.34.120;
    }

So what I'd like to build is a script that would search for 'fixed-address'. If it's followed by '}', do nothing. If it's followed by 'hardware ethernet', flip the lines 'fixed-address' and 'hardware ethernet'.

Does anyone have any advice on how they think that they would accomplish this? I'd appreciate any help that someone could provide.

I don't see any difference between the input file and the desired output!?

My Apologizes.... I have revised the thread... :rolleyes:

What I have tried in various forms is this:

s/(^#)(^host)(^filename)(^fixed)(^hardware)(^})/$1$2$3$5$4$6/

But my regex skills are poor.

Tough to do this with just a regex.

#! /usr/bin/perl

open(INPUT, "<input.data") || die ("Couldn't open input data");
open(OUTPUT, ">output.data") || die ("Couldn't open output data");

my %lines = undef;
my $x = 0;

while(<INPUT>)
{
	$lines{$x++} = $_;
	if(m/\}/)
	{
		if($lines{keys(%lines) - 3} =~ m/hardware ethernet/)
		{
			my $old = $lines{keys(%lines) - 3};
			$lines{keys(%lines) - 3} = $lines{keys(%lines) - 4};
			$lines{keys(%lines) - 4} = $old;
		}

		for($y = 0; $y < keys(%lines); $y++)
		{
			print OUTPUT $lines{$y};
		}

		%lines = undef;
		$x = 0;
	}
}

close(INPUT);
close(OUTPUT);

Thanks DeepakS! That got me closer. I modified the script you sent with my test files.

open(INPUT, "<thing.txt") || die ("Couldn't open input data");
open(OUTPUT, ">thing2.txt") || die ("Couldn't open output data");

So thing2.txt looks like the following:

# Host 1
host 45583 {
    filename "junk1.cm";
    hardware ethernet 11:42:a3:d4:55:83;
    fixed-address 10.100.34.114;
    }
# Host 2
host D78C3 {
    filename "junk5.cm";
    hardware ethernet 11:42:a3:FD:78:C3;
    fixed-address 10.100.34.117;
    }
# Host 3
host 3A684 {
    filename "junk6.cm";
    hardware ethernet 11:42:a3:13:a6:84;
    fixed-address 10.100.34.119;
    }
# Host 4
host 46d54 {
    filename "junk4.cm";
    hardware ethernet 23:10:3d:14:6d:54;
    fixed-address 10.100.34.120;
    }




# Host 1
host 45583 {
    filename "junk1.cm";
    hardware ethernet 11:42:a3:d4:55:83;
    fixed-address 10.100.34.114;
    }
# Host 2
host D78C3 {
    filename "junk5.cm";
    hardware ethernet 11:42:a3:FD:78:C3;
    fixed-address 10.100.34.117;
    }
# Host 3
host 3A684 {
    filename "junk6.cm";
    hardware ethernet 11:42:a3:13:a6:84;
    fixed-address 10.100.34.119;
    }
# Host 4
host 46d54 {
    filename "junk4.cm";
    hardware ethernet 23:10:3d:14:6d:54;
    fixed-address 10.100.34.120;
    }

So the lines are getting manipulated properly, but there is two sets of data now.

---------- Post updated at 10:31 AM ---------- Previous update was at 10:22 AM ----------

Nevermind. I'm a dummy!!! My input file had that.

It is working perfectly!!!! I appreciate all of your input and thanks for the help!!!