Hi cj! =)
On 6/27/06, jhuniepi <[EMAIL PROTECTED]> wrote:
thanks for all the replies. why not use perl? simply because i don't
know perl yet and i want it in bash script. but i dont know how to
read lines and process it. ang gamit ko lang ng read e pag may user
input. di ko lam na pwede pala tong mga script na to.
Actually for this case the simplest Bash and Perl programs for solving
it are quite equivalent, with just a few exceptions. Still, bash can
run out of steam (or more precisely, run out of forks(), even if you
start simple child processes like cut and sed) when dealing with large
volumes of text, which is why Perl or Python is preferred when dealing
with such processing.
As a bit to show how one can do this in Perl, below is one
implementation. It does assume a few things (like assuming text files
are used,) and also does a few bits more than our earlier simple bash
(like write-locking our XML file.) Hope it helps, once you get around
to learning Perl! =)
Cheers,
Zakame
-----BEGIN PERL SCRIPT-----
#!/usr/bin/perl
# transmog - transmogrify some files in a directory
# (Really, just a way to do that in Perl ;)
use strict;
use warnings;
# customize these to suit your work
my $path = "$ENV{HOME}/tmp/test";
my $resultpath = "$ENV{HOME}/tmp/test/result";
my $xmlfile = "$resultpath/test.xml";
# Get our list of "MODEL,,,SERIAL" files in $path
opendir TESTDIR, $path
or die "Can't open $path: $!";
my @files = grep -T, map { "$path/$_" } readdir TESTDIR;
closedir TESTDIR;
# create/update and lock our $xmlfile
use Fcntl qw(:DEFAULT :flock);
sysopen XML, "$xmlfile", O_WRONLY | O_CREAT
or die "Can't create/update file $xmlfile: $!";
flock XML, LOCK_EX
or die "Can't write-lock $xmlfile: $!";
truncate XML, 0
or die "Can't truncate $xmlfile: $!";
# Introductions...
print XML <<EOF;
<?xml name="$xmlfile" ?>
<hello>
EOF
# The real work begins here! =)
my ($model, $serial);
foreach (sort @files) {
# the following line is perhaps redundant, but one never knows...
next unless s#$path/##;
($model, $serial) = /^(\w+),+(\w+)$/;
# This is a convenient setup to make our <data> stuff indented with
# the rest of the code: we assign content into $data, modifying that
# same data with s/// en passant. The `:' is our noise character,
# signifying that the lines bearing it is just data...
#
# The comment inside the <data> tags means that we could still do work
# there (perhaps opening $_ and extract the juicy bits...)
(my $data = <<" ENTRY") =~ s/^\s+:\t+//gm;
: <data model="$model" serial="$serial">
: # do something useful with $model and $serial here
: </data>
ENTRY
print XML $data;
}
# Conclusion
print XML "</hello>\n";
close XML;
# We're done if we get here
print "ok\n";
-----END PERL SCRIPT-----
--
Zak B. Elep || http://zakame.spunge.org
[EMAIL PROTECTED] || [EMAIL PROTECTED]
1486 7957 454D E529 E4F1 F75E 5787 B1FD FA53 851D
_________________________________________________
Philippine Linux Users' Group (PLUG) Mailing List
[email protected] (#PLUG @ irc.free.net.ph)
Read the Guidelines: http://linux.org.ph/lists
Searchable Archives: http://archives.free.net.ph