z-every-line.pl 589 B

123456789101112131415161718192021222324
  1. #!/usr/bin/perl -i
  2. use strict;
  3. use warnings;
  4. my $z = 0;
  5. # read stdin and any/all files passed as parameters one line at a time
  6. while (<>) {
  7. # if we find a Z word, save it
  8. $z = $1 if /Z\s*(\d+(\.\d+)?)/;
  9. # if we don't have Z, but we do have X and Y
  10. if (!/Z/ && /X/ && /Y/ && $z > 0) {
  11. # chop off the end of the line (incl. comments), saving chopped section in $1
  12. s/\s*([\r\n\;\(].*)/" Z$z $1"/es;
  13. # print start of line, insert our Z value then re-add the chopped end of line
  14. # print "$_ Z$z $1";
  15. }
  16. #else {
  17. # nothing interesting, print line as-is
  18. print or die $!;
  19. #}
  20. }