Simple text file operation

L

logic1

Guys I have a bit of an issue.

I collect a series of text files on a daily basis. About 48 in total
per day.

The naming format on these files is "<date>_<version_num>_AT*<1 to
10>_asc.blob"

The contents are in the two column format:

---------------------------------------
referencenumber | value
referencenumber | value
referencenumber | value
referencenumber | value
referencenumber | value
---------------------------------------


I need to alter these text files to include two new columns from the
filename itself.

So the file looks like: (Both date and version_num are taken from the
filename and put into the text file)

-------------------------------------------------------------
date | version_num | referencenumber | Value
date | version_num | referencenumber | Value
date | version_num | referencenumber | Value
date | version_num | referencenumber | Value
date | version_num | referencenumber | Value
 
A

Anno Siegel

logic1 said:
Guys I have a bit of an issue.

I collect a series of text files on a daily basis. About 48 in total
per day.

The naming format on these files is "<date>_<version_num>_AT*<1 to
10>_asc.blob"

The contents are in the two column format:

---------------------------------------
referencenumber | value
referencenumber | value
referencenumber | value
referencenumber | value
referencenumber | value
---------------------------------------


I need to alter these text files to include two new columns from the
filename itself.

So the file looks like: (Both date and version_num are taken from the
filename and put into the text file)

-------------------------------------------------------------
date | version_num | referencenumber | Value
date | version_num | referencenumber | Value
date | version_num | referencenumber | Value
date | version_num | referencenumber | Value
date | version_num | referencenumber | Value
--------------------------------------------------------------

What have you tried so far?

We can help you with a script, but we're not in the habit of writing
complete solutions from scratch.

Anno
 
T

Tad McClellan

logic1 said:
Guys I have a bit of an issue.


If you state what your issue is, we have a much better chance
of being able to help you overcome it.

It is easiest for us to help you if you have question marks
at the end of one or more sentences.

What parts do you already have working?

What parts are you having trouble with?

I need to alter these text files


Have you seen the Perl FAQ about changing the contents of files?

perldoc -q file

How do I change one line in a file/delete a line in a
file/insert a line in the middle of a file/append to the
beginning of a file?

Any help appreciated.


Ask a question and we will (likely) respond with an answer.
 
L

logic1

What have you tried so far?

We can help you with a script, but we're not in the habit of writing
complete solutions from scratch.

Anno

Hi,

Sorry about that I did mean to include the shell script I had so far.

It's basically pseudo code as my perl knowledge is extremly limited
and I'm trying to learn as I go.

for $file in `ls *.blob` do
date=`echo $file `gawk -F '{ print $1 }'`
ver=`echo $file `gawk -F '{ print $2 }'`
for line in `cat $file`
do
echo "$date $ver $line";


There's obviously numerous errors in there in syntax and language but
as I said my perl knowledge is limited and I need this solution fairly
quickly.

..logic.
 
A

Anno Siegel

logic1 said:
(e-mail address removed)-berlin.de (Anno Siegel) wrote in message


Hi,

Sorry about that I did mean to include the shell script I had so far.

It's basically pseudo code as my perl knowledge is extremly limited
and I'm trying to learn as I go.

for $file in `ls *.blob` do
date=`echo $file `gawk -F '{ print $1 }'`
ver=`echo $file `gawk -F '{ print $2 }'`
for line in `cat $file`
do
echo "$date $ver $line";

That's a shell script. This is a Perl group.
There's obviously numerous errors in there in syntax and language but
as I said my perl knowledge is limited and I need this solution fairly
quickly.

Doesn't everybody...

Here is a solution. See if you can use it.

my $dir = '/tmp/aux';
opendir my $d, $dir or die "Can't read directory $dir: $!";

for my $file ( grep /\.blob$/, readdir( $d) ) {
my ( $date, $ver, $n) = $file =~ /([^_]*)_([^_]*)_AT.*(\d+)/;
my $f;
unless ( open $f, "$dir/$file" ) {
warn "Can't read $dir/$file: $!";
next;
}
while ( <$f> ) {
chomp;
my ( $ref, $val) = split /\s*\|\s*/;
print "$date | $ver | $ref | $val\n"
}
}

Anno
 
L

logic1

Anno,

Thank you for your reply. That script is most useful. Apologies for my
Perl usergroup faux pas'.

thanks again,

..logic.

That's a shell script. This is a Perl group.
There's obviously numerous errors in there in syntax and language but
as I said my perl knowledge is limited and I need this solution fairly
quickly.

Doesn't everybody...

Here is a solution. See if you can use it.

my $dir = '/tmp/aux';
opendir my $d, $dir or die "Can't read directory $dir: $!";

for my $file ( grep /\.blob$/, readdir( $d) ) {
my ( $date, $ver, $n) = $file =~ /([^_]*)_([^_]*)_AT.*(\d+)/;
my $f;
unless ( open $f, "$dir/$file" ) {
warn "Can't read $dir/$file: $!";
next;
}
while ( <$f> ) {
chomp;
my ( $ref, $val) = split /\s*\|\s*/;
print "$date | $ver | $ref | $val\n"
}
}

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top