CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [Kdawg01] Copying a file and modifying line order output: Edit Log



Kenosis
User

Mar 23, 2013, 11:57 AM


Views: 460
Re: [Kdawg01] Copying a file and modifying line order output

Perhaps the following will be helpful:


Code
use strict; 
use warnings;

my $lines;
while (<>) {
/n\d+00/ and $lines .= $_ and next;
if ( !index $_, 'm6' ) {
print $_, $lines;
$lines = '';
}
elsif ($lines) {
$lines .= $_;
}
else {
print;
}
}


Usage: perl script.pl inFile [>outFile]

Dataset:


Code
text0 
text1
n700
text 1
text 2
m6_foobar
n400
text3_3
text 4
m6_ohmy
n800
text 5
text 6
m6_1234
text3
text4


Output:


Code
text0 
text1
m6_foobar
n700
text 1
text 2
m6_ohmy
n400
text3_3
text 4
m6_1234
n800
text 5
text 6
text3


Use the last, optional argument to redirect output to a file.

Hope this helps!

Edit: Solution's been updated to meet the OP's specs. Thanks, Laurent_R!


(This post was edited by Kenosis on Mar 24, 2013, 12:48 AM)


Edit Log:
Post edited by Kenosis (User) on Mar 23, 2013, 12:02 PM
Post edited by Kenosis (User) on Mar 23, 2013, 8:29 PM
Post edited by Kenosis (User) on Mar 23, 2013, 9:17 PM
Post edited by Kenosis (User) on Mar 23, 2013, 11:49 PM
Post edited by Kenosis (User) on Mar 23, 2013, 11:53 PM
Post edited by Kenosis (User) on Mar 24, 2013, 12:48 AM
Post edited by Kenosis (User) on Mar 24, 2013, 12:48 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives