Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Adding a TAB after a certain ammount of characters

by biancoari (Initiate)
on Apr 16, 2013 at 20:30 UTC ( [id://1029006]=perlquestion: print w/replies, xml ) Need Help??

biancoari has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I need your assistance regarding coding, I have a txt file with several lines like this one "2013020400000000006810083610022013068100200220130688000200494" what I need to do is add a TAB after the first 17 numbers, another one after the next 6 numbers, another one after the next 31 numbers, another one after the next 3 and another after the other 3, in every line, anyone can please guide me on how to complete this task? Thanks for the assistance guys

Replies are listed 'Best First'.
Re: Adding a TAB after a certain ammount of characters
by BrowserUk (Patriarch) on Apr 16, 2013 at 21:21 UTC

    perl -pe"$_ = join qq[\t], unpack 'a17 a6 a31 a3 a3', $_" infile > out +file

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Adding a TAB after a certain ammount of characters
by kennethk (Abbot) on Apr 16, 2013 at 20:33 UTC
    xkcd://208: Regular Expressions to the rescue.
    perl -pi -e 's/^(.{17})(.{6})(.{31})(.{3})(.{3})/$1\t$2\t$3\t$4\t$5\t/ +' file.txt

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Adding a TAB after a certain ammount of characters
by choroba (Cardinal) on Apr 16, 2013 at 21:08 UTC
    You can use substr to change a string:
    my $string = "20130204000000000068100836100220130681002002201306880002 +00494"; my $n = -1; substr $string, $n += 1 + $_, 0, "\t" for 17, 6, 31, 3, 3; print "$string\n";
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Attacking the insertions in reverse order from the right-hand side means you don't have to increment your position by one for each successive insertion. Swings and roundabouts really but sometimes a useful technique.

      $ perl -Mstrict -Mwarnings -E ' > my $text = > q{2013020400000000006810083610022013068100200220130688000200494}; > my @offs = ( 17, 6, 31, 3, 3 ); > my @cums = ( 0 ); > push @cums, $_ + $cums[ -1 ] for @offs; > substr $text, $_, 0, q{-} for reverse @cums[ 1 .. $#cums ]; > say $text;' 20130204000000000-068100-8361002201306810020022013068800-020-049-4

      Here's the code without the PS2 prompt for easier copy'n'paste, as requested by /msg in respect of another post.

      I hope this is of interest.

      Cheers,

      JohnGG

        Hi, I've tried to use it exactly as you put it and I got:

        biancoari@biancoari ~/Desktop $ perl perl1.pl
        String found where operator expected at perl1.pl line 8, near "say $text;'"
        (Might be a runaway multi-line '' string starting on line 1)
        (Missing semicolon on previous line?)
        syntax error at perl1.pl line 8, near "say $text;'"
        Execution of perl1.pl aborted due to compilation errors.

        Besides I have another question what I am trying to modify is a txt file with several lines (like the one on the example, they are all the same) any clue on that?

Re: Adding a TAB after a certain ammount of characters
by BillKSmith (Monsignor) on Apr 17, 2013 at 02:42 UTC
    The template for the function unpack is a perfect match to your description of the problem.
    use strict; use warnings; my $template = "A17A6A31A3A3A*"; while (my $line = <DATA>) { print join("\t", unpack($template, $line)); } __DATA__ 2013020400000000006810083610022013068100200220130688000200494;
    Bill

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-03-29 01:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found