Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by benlaw (Scribe)
on Jan 24, 2006 at 08:35 UTC ( #525126=perlquestion: print w/ replies, xml ) Need Help??
benlaw has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have data like this
00000001 99888 00000011 99889 00000002 99890 00000003 99808 (m +any many...) 00000021 99888 00000021 99889 00000022 99890 00000023 99900 (many many)
I would like grep like
00000001 99888 00000011 99889 00000002 99890 ....
How can i do that, Thanks a lot!

Comment on repeat data at one line and grep as pre-set format
Select or Download Code
Re: repeat data at one line and grep as pre-set format
by davido (Archbishop) on Jan 24, 2006 at 08:49 UTC

    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

      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

Re: repeat data at one line and grep as pre-set format
by saintmike (Vicar) on Jan 24, 2006 at 08:50 UTC
    Are you saying you want the first two fields extracted from every line? If so, just use split:
    while(<DATA>) { my @two = split ' ', $_; print "@two[0,1]\n"; } __DATA__ 00000001 99888 00000011 99889 00000002 99890 00000003 99808 00000021 99888 00000021 99889 00000022 99890 00000023 99900
Re: repeat data at one line and grep as pre-set format
by svenXY (Deacon) on Jan 24, 2006 at 08:51 UTC
    Hi,
    perl -n -e'print join(" ", (split(/\s+/))[0,1]), "\n"' yourfile.txt

    Regards,
    svenXY
    Update: fixed the line, works now
Re: repeat data at one line and grep as pre-set format
by demerphq (Chancellor) on Jan 24, 2006 at 08:55 UTC
    perl -aF/\s+/ -lne"print join qq(\t),splice(@F,0,2) while @F" t.txt

    Adjust the quotes to suite your shell.

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

      Great thanks!!

        Its not at all clear what you meant by "grep" in this context. Nothing is being grepped.

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

Re: repeat data at one line and grep as pre-set format
by Random_Walk (Parson) on Jan 24, 2006 at 15:15 UTC

    I think you want to split each pair of data points out to a line on its own instead of having multiple datapoints on one line.

    cat yourfile.txt | perl -ple 's/(\S+\s+\S+)\s+/$1\n/g'

    should do this for you as long as you have even numbers of datapoints on each line

    echo "00000001 99888 00000011 99889 00000002 99890 00000003 998 +08" \ | perl -ple 's/(\S+\s+\S+)\s+/$1\n/g' 00000001 99888 00000011 99889 00000002 99890 00000003 99808

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
      cat yourfile.txt | perl -ple 's/(\S+\s+\S+)\s+/$1\n/g'

      Isn't that the same as:

      perl -ple 's/(\S+\s+\S+)\s+/$1\n/g' yourfile.txt

      UUOC.


      Dave

        You are right and your way is more efficient too. I wrote it with the cat file | as that is how I tested it with echo, just failed to engage my brain completely :-)

        Cheers,
        R.

        Pereant, qui ante nos nostra dixerunt!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://525126]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-12-20 23:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (99 votes), past polls