ContributedMaterial: totl2blnd

File totl2blnd, 1.2 KB (added by mchatzikos, 4 years ago)

Perl script searches all input files (*.in) for a monitor command at the requested wavelength, and replaces the keyword 'totl' with 'blnd'.

Line 
1#!/usr/bin/perl
2############################################################
3#
4# totl2blnd:
5#       In current directory, find monitor lines of the
6# requested wavelength, and replace the keyword 'totl' with
7# 'blnd'.
8#
9# Chatzikos, Marios                             2013-Apr-24
10#
11############################################################
12
13use warnings;
14use strict;
15
16
17
18
19my $wavelength = shift @ARGV;
20
21die "Usage: $0 <wavelength>\n"
22        if (not defined($wavelength));
23
24
25my @input_files = glob("*.in");
26die "\tNo input files (*.in) found in current directory!\n"
27        if (scalar(@input_files) == 0);
28
29
30foreach my $input (@input_files)
31{
32        # Get and modify the input script
33        #
34        open INPUT, "< $input"
35          or die "Error: Could not open: $input\n";
36
37        my @output;
38        my $nchange = 0;
39        while (my $line = <INPUT>)
40        {
41                if ($line =~ m/^monitor .* "(totl|TOTL)"\s+$wavelength/)
42                {
43                        #       print $line;
44                        $line =~ s/\"(totl|TOTL)\"/\"Blnd\"/;
45                        $nchange++;
46                        #       print $line;
47                }
48                push (@output, $line);
49        }
50
51        close INPUT
52           or warn "Warning: Could not close: $input\n";
53
54
55        next    if ($nchange == 0);
56
57
58        # Overwrite file
59        open OUTPUT, "> $input"
60          or die "Error: Could not open: $input\n";
61
62        print OUTPUT @output;
63
64        close OUTPUT
65           or warn "Warning: Could not close: $input\n";
66
67        print "\t Updated: $input\n";
68}