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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting swapping lines that match a condition using sed, perl or the like
# 1  
Old 09-10-2009
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...

Code:
# 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...

Code:
# 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.

Last edited by TheBigAmbulance; 09-10-2009 at 11:33 AM..
# 2  
Old 09-10-2009
I don't see any difference between the input file and the desired output!?
# 3  
Old 09-10-2009
My Apologizes.... I have revised the thread... Smilie

What I have tried in various forms is this:
Code:
s/(^#)(^host)(^filename)(^fixed)(^hardware)(^})/$1$2$3$5$4$6/

But my regex skills are poor.

Last edited by TheBigAmbulance; 09-10-2009 at 12:02 PM..
# 4  
Old 09-10-2009
Tough to do this with just a regex.

Code:
#! /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);

# 5  
Old 09-10-2009
Thanks DeepakS! That got me closer. I modified the script you sent with my test files.
Code:
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:
Code:
# 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!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed read X lines after match

I want to do something like sed -n '/PATTERN/,+10p' and get the ten lines following PATTERN. However, this throws an "expected context address" with the sed that comes with OSX Lion. If that + is a GNUism, can I do this, or do I have to find another tool? (2 Replies)
Discussion started by: jnojr
2 Replies

2. UNIX for Dummies Questions & Answers

sed, join lines that do not match pattern

Hello, Could someone help me with sed. I have searched for solution 5 days allready :wall:, but cant find. Unfortunately my "sed" knowledge not good enough to manage it. I have the text: 123, foo1, bar1, short text1, dat1e, stable_pattern 124, foo2, bar2, long text with few lines, date,... (4 Replies)
Discussion started by: petrasl
4 Replies

3. Shell Programming and Scripting

Insert few lines above a match using sed, and within a perl file.

Greetings all, I am trying to match a string, and after that insert a few lines above that match. The string is "Version 1.0.0". I need to insert a few lines ONLY above the first match (there are many Version numbers in the file). The rest of the matches must be ignored. The lines I need to... (2 Replies)
Discussion started by: nagaraj s
2 Replies

4. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

5. Shell Programming and Scripting

perl or awk remove empty lines when condition

Hi Everyone, # cat 1 a b b cc 1 2 3 3 3 4 55 5 a b (2 Replies)
Discussion started by: jimmy_y
2 Replies

6. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 Replies

7. Shell Programming and Scripting

sed problem - delete all lines until a match on 2 lines

First of all, I know this can be more eassily done with perl or other scripting languages but, that's not the issue. I need this in sed. (or wander if it's possible ) I got a file (trace file to recreate the control file from oracle for the dba boys) which contains some lines another line... (11 Replies)
Discussion started by: plelie2
11 Replies

8. Shell Programming and Scripting

Swapping lines beginning with certain words using sed/awk

I have a large file which reads like this: fixed-address 192.168.6.6 { hardware ethernet 00:22:64:5b:db:b1; host X; } fixed-address 192.168.6.7 { hardware ethernet 00:22:64:5b:db:b3; host Y; } fixed-address 192.168.6.8 { hardware ethernet 00:22:64:5b:db:b4; host A; }... (4 Replies)
Discussion started by: ksk
4 Replies

9. Shell Programming and Scripting

Swapping or switching 2 lines using sed

I made a script that can swap info on two lines using a combination of awk and sed, but was hoping to consolidate the script to make it run faster. If found this script, but can't seem to get it to work in a bash shell. I keep getting the error "Too many {'s". Any help here would be appreciated:... (38 Replies)
Discussion started by: LaTortuga
38 Replies

10. Shell Programming and Scripting

read and match multiple lines in perl

Could any one tell me how to read and match multiple lines in perl? Did this code below still work in this situation? while (<FILE>) { if (/ /) { } } Thanks a lot! (5 Replies)
Discussion started by: zx1106
5 Replies
Login or Register to Ask a Question