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;'