On 28 June 2011 16:08, Kent Fredric <[email protected]> wrote:

> echo abcdefghijklmnopqrstuvwxyz | perl -ple 'substr( $_, 5 ) =~ 
> s/(.{4})/$1Z/g;'

Oh yeah, in case that <ARGV> stuff wasn't clear enough, this will also work:

echo abcdefghijklmnopqrstuvwxyz > file
perl -ple 'substr( $_, 5 ) =~ s/(.{4})/$1Z/g;'  file

And incidentally,

perl -i.bak -ple 'substr( $_, 5 ) =~ s/(.{4})/$1Z/g;'  file

will back 'file' up as 'file.bak' and emit 'file' with the
substitutions provided ( like sed -i )

However, I'd generally recommend against using -i , despite it having
occasional practical use.

>
> abcdefghiZjklmZnopqZrstuZvwxyZz
>
> ^ works, but I'd advise against' relying wholly on oneliners.
>
> Also, at present, that works linewise, ie: each line will start over,
> skip 5 characters, etc.
>
> If you wanted it to work on the entire file like that, it would
> require a little modification.
>
> Code Expansion:
>
> -ple 'foo'
>
> is shorthand for
>
> # ARGV is either a stream of all input from all files listed as arguments,
> # or the contents of STDIN
> while( defined ( $_ = <ARGV> ) ) {
>       chomp $_ # trim \n from end.
>        foo # <-- code goes here
>        print $_, "\n";
> }
>
> substr( $_, 5 ) =~
>
> does an in-place modification of $_ , allowing the right hand side of
> the =~ operator to modify all characters except the first 5.
>
> =~ s/(.{4})/$1Z/g
>
> walks over the string an injects a 'Z' after each 4th character. (
> Capture a group of 4 characters, replace it with that captured group
> followed by a Z, repeat )
>


-- 
Kent

perl -e  "print substr( \"edrgmaM  SPA NOcomil.ic\\@tfrken\", \$_ * 3,
3 ) for ( 9,8,0,7,1,6,5,4,3,2 );"

http://kent-fredric.fox.geek.nz

_______________________________________________
Linux-users mailing list
[email protected]
http://lists.canterbury.ac.nz/mailman/listinfo/linux-users

Reply via email to