Skip to content

Instantly share code, notes, and snippets.

@zengargoyle
Last active February 16, 2019 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zengargoyle/231a642f6cf2f97c6d0911ae387cf36d to your computer and use it in GitHub Desktop.
Save zengargoyle/231a642f6cf2f97c6d0911ae387cf36d to your computer and use it in GitHub Desktop.
make some sort of list thingy
#!/usr/bin/perl
use strict;
use warnings;
use autodie;
use utf8::all;
use Clipboard;
# Usage:
#
# foo-that <list1> <string to match> <list2> [output file]
#
# if no output file is given place in Clipboard
#
my ($input_file, $match_text, $replacements_file, $output_file) = @ARGV;
my $fh;
# read list1 and chomp off newlines
open $fh, '<', $input_file;
my @input_lines = <$fh>;
chomp @input_lines;
# read list2 and chomp off newlines
open $fh, '<', $replacements_file;
my @replacements = <$fh>;
chomp @replacements;
# make sure we have same number of lines in each list1, list2
@input_lines == @replacements or die "Unequal line counts!\n";
my @output_lines;
my $count;
# do the line by line replacements
while (my $line = shift @input_lines) {
my $replacement = shift @replacements;
$count++;
$line =~ s/$match_text/$replacement/
or die "Match Text not found line $count";
push @output_lines, $line;
}
# build output by inserting newlines and such
my $output_text = join "\n", @output_lines;
$output_text .= "\n";
# print to the list3 or ... send it to the Clipboard
if ($output_file) {
open $fh, '>', $output_file;
print $fh $output_text;
}
else {
Clipboard->copy($output_text);
}
=AREA(C='Plants_-AbcDef')
=AREA(C='Plants_-AbcDef')
=AREA(C='Plants_-AbcDef')
=AREA(C='Plants_-AbcDef')
=AREA(C='Plants_-AbcDef')
GriLit
HebSal
MelLan
MelRam
MyrMel
$ ./foome list1 AbcDef replacements ; xclip -o
=AREA(C='Plants_-GriLit')
=AREA(C='Plants_-HebSal')
=AREA(C='Plants_-MelLan')
=AREA(C='Plants_-MelRam')
=AREA(C='Plants_-MyrMel')
$ ./foome list1 AbcDef replacements list3 ; cat list3
=AREA(C='Plants_-GriLit')
=AREA(C='Plants_-HebSal')
=AREA(C='Plants_-MelLan')
=AREA(C='Plants_-MelRam')
=AREA(C='Plants_-MyrMel')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment