Can "perl -a" somehow rejoin @F using the original spaces?

My input has a combination of tabs and spaces for readability. I want to change the field with perl -aand then print the line in its original form. (Data from findup, showing me the number of duplicate files and the space they spend.) Input:

2 * 4096    backup/photos/photo.jpg photos/photo.jpg
2 * 111276032   backup/books/book.pdf book.pdf

The output converts field 3 to kilobytes, for example:

2 * 4 KB    backup/photos/photo.jpg photos/photo.jpg
2 * 108668 KB   backup/books/book.pdf book.pdf

In the world of my dreams, this will be my code, since I could just rewrite the recombination automatically @Fand keep the original spaces:

perl -lanE '$F[2]=int($F[2]/1024)." KB"; print;'

In real life, connecting to one space seems to be my only option:

perl -lanE '$F[2]=int($F[2]/1024)." KB"; print join(" ", @F);'

Is there any automatic variable that remembers separators? If I had a magic array, the code would be as follows:

perl -lanE 'BEGIN{use List::Util "reduce";} $F[2]=int($F[2]/1024)." KB"; print reduce { $a . shift(@magic) . $b } @F;'
+6
2

, . ,

perl -wnE'@p = split /(\s+)/; $p[4] = int($p[4]/1024); print @p' input.txt

parens split , , . , .

, -F . 27.

perl -F'(\s+)' -lanE'$F[4] = int($F[4]/1024); say @F' input.txt

: 5.20.0 "-F -a -a -n". ysth.

+9

:

perl -wpE's/^\s*+(?>\S+\s+){2}\K(\S+)/int($1\/1024) . " KB"/e'
+1

All Articles