SuicideJunkie's approach is very compact. An alternative approach (not nearly as compact or elegant, but perhaps more straightforward) still uses regular expressions (Regex's) but does the swapping in a somewhat more pedestrian fashion is shown below:

#!/user/bin/perl use strict; # use warnings; # SETUP THE FILES FOR THIS DEMO ##################################################### # Define the working directory for files my $dir = 'c:/Documents and Settings/myname/Desktop/'; # Define the filename to put OP's input my $infilename = 'TestCase.txt'; # Define the filename to write the results into my $outfilename = 'TestCaseOut.txt'; # Create the full-path filenames for input and output my $infile = $dir . $infilename; my $outfile = $dir . $outfilename; open(INFILE,"<",$infile) || die "Can't open input file $infilename: $!\n"; open(OUTFILE,">",$outfile) || die "Can't open output file $outfilename: $!\n"; #################################################### # PROCESS THE INPUT my $line = ""; # Initialize variable to hold each line # read in from the input file my $prior_line = ""; # Initialize variable to hold the # previous line (will need for the # swap if need to do the swap # Process each line as it is read in, always keeping track # of previous line (in case need to do a swap foreach $line (<INFILE>){ chomp($line); # remove the trailing '\n' new-line # Per OP's strategy, check to see if the prior line was # a 'fixed-address' entry...using a regex if($prior_line =~ /^fixed-address/){ # if the prior line was, indeed a 'fixed-address' then # check to see if this line is a 'hardware ethernet', # if it is then do the OP's swap using Perl's # ability to do the swap in-place ($line,$prior_line) = ($prior_line,$line) if($line =~ /^hardware ethernet/); } # I write the output to the output file on each # iteration, but since I am effectively doing a sort of # look-ahead one line at a time, I only write the line # that I'm finished with...i.e., the $prior_line print OUTFILE "$prior_line","\n"; # Now, remember the line I just read in so that it # becomes the $prior_line $prior_line = $line; } # When I've read in and processed the entire file, I # have written out all of the lines except the last one # I read in...which the above loop just moved into # $prior_line. So I need to write it out to the output # file to complete the effort. print OUTFILE "$prior_line","\n"; exit(0);

Given the OP's input, the output 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; }

I hope this helps. I tried to comment the code so that the OP can see the steps and the logic in a way that someone who is not too familiar with Perl can follow. I may have misjudged the OP's level of familiarity with Perl; if I have, I appologize.

ack Albuquerque, NM

In reply to Re: swapping lines that match a condition by ack
in thread swapping lines that match a condition by TheBigAmbulance

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.