in reply to repeat data at one line and grep as pre-set format

I'm not entirely sure what you're asking for, but it looks like you're just needing to reformat the data to a two-column output. If that's the case, this ought to do the trick:

while( my $line = <DATA> ) { while( $line =~ m/(\d{8})\s+(\d{5})/g ) { print "$1 $2\n"; } }

If I've missed the point to the question, you might help out by providing further explanation on what you are trying to do.


Dave

Replies are listed 'Best First'.
Re^2: repeat data at one line and grep as pre-set format
by demerphq (Chancellor) on Jan 24, 2006 at 09:13 UTC

    Imo if you are going to do that then you should just do an s///

    perl -pe "s/(\d{8}\s+\d{5})(\s+)/$1\n/g" t.txt

    In sure there is a way to do that without needing to copy $1, but I couldnt work it out quickly enough to post.

    ---
    $world=~s/war/peace/g

      You could avoid copying $1 if you use lookbehind, but there is that pesky problem of \s+ between the digit fields, which won't work with lookbehind (variable width rule). I couldn't tell by the OP's post whether there was a fixed number of spaces or not. Assuming only two spaces (or at least a known quantity of spaces) between the column fields, you could do something like this:

      perl -pe "s/(?<=\d{8}\s\s\d{5})\s+/\n/g;" t.txt

      Dave

        You could use a combination of lookbehind and lookahead:

        perl -pe "s[(?<=\d{5})\s+(?=\d{8})][\n]g" t.txt

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.