Link to home
Create AccountLog in
Avatar of saibsk
saibsk

asked on

perl script padding and resizing

I am retrieving customer id from a file where the customer ids are stored in this format.
id
1
2
3
4
12


But in the database where the customer name is stored it is stored as
cust_id      name
cust001 john
cust012 harry

where cust001 is 1 from file
and cust012 is 12 from file

I need to do the mapping 1 -> cust001. I know when the padding is fixed but here it needs resizing as the id could be 1 or 12 or 123. But the maximum length of customer id is 3 digits.
I need to append the cust_id and the name to the id in the file using Perl.
Avatar of Adam314
Adam314

If you want to format 1 as "cust001", then use:
    $cust_id_db = sprintf("cust%03d", $cust_id);
Avatar of saibsk

ASKER

I have small question:

When I am getting the data file the data is actually in this format

original format
1
new york new jersey

when actually it should be
1 new york new jersey

do i parse the original file and write to a different file. Or is there any other optimized way to do this?
Avatar of ozo
Is it always 2 lines?
Is it always a number and then something that is not a number?
Avatar of saibsk

ASKER

i have 2 files with this problem. One of them is something like this:

6
10:54:46 pm 10:54:46 pm

8
10:55:08 pm 10:55:08 pm

If I know how to work on this one I can write for the other one.
ASKER CERTIFIED SOLUTION
Avatar of Adam314
Adam314

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Is it always 2 lines then a blank line?
Are the numbers you want always the only thing on their lines?
Are all numbers that are alone on a line numbers that you want?
Avatar of saibsk

ASKER

Yes there is blank line after the 2 lines.
yes there only the numbers I want on the line, followed by 2 timings on the next line.
I didn't get the 3rd question.

Additionally there could be headers something like this
customer
--------------------------------------------------------------------------------
min_now   max_now

And the code doesn't seem to work doesn't print anything
Put the code in a file, use like:
    file.pl original.txt > new.txt
in MS windows
perl -ne "printf qq'cust%03d\n',$1 if /^(\d+)$/" original.txt
in Unix
perl -ne 'printf"cust%03d\n",$1 if /^(\d+)$/' original.txt
Avatar of saibsk

ASKER

I want the numbers followed by the two timings on the same line
Avatar of saibsk

ASKER

>Put the code in a file, use like:
    file.pl original.txt > new.txt

When i write it to file and execute it, looks fine.

But I need to add this to my existing script so I did something like this;

open FH,"<tlm_al.txt";

local $/='';
while($ln = <FH>){
$ln =~ s/\n/ /sg;
print "$_\n";
}

It doesn't print anything. WHere am  I doing it wrong?
open FH,"<tlm_al.txt" or die "can't open tlm_al.txt $!";
while( <FH> ){
   printf"cust%03d\n",$1 if /^\s*(\d+)\s*$/;
}
Avatar of saibsk

ASKER

But I need something like this:

cust006 10:54:46 pm 10:54:46 pm

cust008 10:55:08 pm 10:55:08 pm

The code:
local $/='';
while(<>) {
      s/\n/ /sg;
      print "$_\n";
}

does this

6 10:54:46 pm 10:54:46 pm

8 10:55:08 pm 10:55:08 pm

But I need to map  the 6 & 8 to database. There for the conversion to client006. But when I parse the file it considers the above two files as one line.
local $/='';
while(<>) {
      s/\n/ /sg;
      printf "cust%03d %s\n", split(/s+/, $_, 2);
}

Avatar of saibsk

ASKER

I need the timings too I tried something like this

while(<read_tlm>){
chomp;
print "LINE: $ln\n";
$id = $1 and next if /\s*(\d{1,3})\s+$/;
$start = $1 and next if /^(\d{2}\:\d{2}\:{2}).*/;
$end = $2 and next if /^(\d{2}\:\d{2}\:{2}).*?(\d{2}\:\d{2}\:{2}).*/;
print "NEXT: $id\n";
}

But doesn't print anything.

Avatar of saibsk

ASKER

I think I figured it out. Thanks.