#!/usr/bin/perl -w use warnings; use strict; my $record_element = 'dcterms:available'; my $filename_element = 'dc:identifier'; my ($file, $dir) = @ARGV; defined($file) && defined($dir) || usage(); chop($dir) if $dir =~ m#/$#; #$/ = "</$record_element>"; mkdir $dir; open(INPUT, "<$ARGV[0]") || die "Failed to open $ARGV[0]\n"; $file = "INPUT"; my $record_count = 0; my $header1 = '<?xml version="1.0" encoding="UTF-8"?>'; my $header2 = '<qualifieddc xmlns="http://example.org/appqualifieddc/" +'; my $header3 = 'xmlns:dc="http://purl.org/dc/elements/1.1/"'; my $header4 = 'xmlns:dcterms="http://purl.org/dc/terms/"'; my $header5 = 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'; my $header6 = 'xsi:schemaLocation="http://example.org/appqualifieddc/' +; my $header7 = 'http://dublincore.org/schemas/xmls/qdc/2003/04/02/appqu +alifieddc.xsd"'; my $closing = '</qualifieddc>'; $/ ='terms/">'; <INPUT>; $/ = "</$record_element>"; while(<INPUT>){ next if m/^\s*$/; #Disregard 0+ trailing spaces s/^\s*//; #Collapse 0+ leading spaces $record_count++; if (m#<$filename_element xsi:type="dcterms:URI">(\w+) (\w+) (\w+)_(\ +w+)</$filename_element>#s) { msg("Just matched $1 $2 $3_$4"); my $outfile = "$dir/$4.xml"; open (OUTPUT, '>', $outfile) or die "Can't create/open $outfile fo +r writing"; print OUTPUT $header1, "\n", $header2, "\n", $header3, "\n", $ +header4, "\n", $header5, "\n", $header6, "\n", $header7, "\n"; print OUTPUT $_, "\n"; print OUTPUT $closing; close (OUTPUT) or die "Couldn't close $outfile after writing recor +d"; } else { warn("Couldn't find filename in record $record_count, skipping..." +); } } close (INPUT) or die "Couldn't close $file"; close (OUTFILE); $record_count--; msg("Processed $record_count records from $file"); sub msg { print @_, "\n"; } sub usage { msg("Usage: $0 <file> <directory>"); exit(1); }
In reply to Read/Change files in a Directory by LF
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |