Wow, Perl is really cool when it works! Thanks for your help!!
Here's my code:
#!/usr/bin/perl -w
use strict;
open(INPUTFILE, "< input.txt") or die "cannot open file for reading $!";
open(OUTPUTFILE, "> output.txt") or die "cannot open file for writing $!";
foreach my $line (<INPUTFILE>)
{
$line =~ s/&+/&/;
# you can also use my (...) to declare many variables
# at once
my ($isbn, $ocln, $title, $author, $call_number, $publish_date, $lccn, $request_number)
= split(/\t/, $line);
# you'll likely have different defaults for cases where
# fields are undefined
$isbn='' unless defined($isbn);
$ocln='' unless defined($ocln);
$title='' unless defined($title);
$author='' unless defined($author);
$call_number='' unless defined($call_number);
$publish_date='' unless defined($publish_date);
$lccn='' unless defined($lccn);
$request_number='' unless defined($request_number);
print OUTPUTFILE "<match>\n";
print OUTPUTFILE "<title>";
print OUTPUTFILE $title, "</title>\n";
print OUTPUTFILE "<isbn>";
print OUTPUTFILE $isbn, "</isbn>\n";
print OUTPUTFILE "<call_number>";
print OUTPUTFILE $call_number,"</call_number>";
print OUTPUTFILE "</match>\n";
}
close INPUTFILE;
close OUTPUTFILE;
Here's a sample of the output:
$ tail output.txt
<isbn>313228841</isbn>
<call_number>Z8424.D69</call_number></match>
<match>
<title>"Blogs, Wikipedia, Second life, and Beyond : from production to produsage / Axel Bruns."</title>
<isbn>820488674</isbn>
<call_number>ZA4482 .B78 2008</call_number></match>
<match>
<title>"Living standards in the past : new perspectives on well-being in Asia and Europe / edited by Robert C. Allen, Tommy Bengtsson, and Martin Dribe."</title>
<isbn>199280681</isbn>
<call_number>zHD7048.L58 2005</call_number></match>
Thanks again! Now I'm off to build on this beginning ...
Best,
Libmonk
In reply to Re^2: input tab delimited file
by libmonk
in thread input tab delimited file
by libmonk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |