Hi! =)

On 7/5/06, Mehdi Aboulkassim <[EMAIL PROTECTED]> wrote:

Hi Ariz,

Yes it has to be written in bash, but if impossible?! I can use onother one.

Here is a sample of my flat file input and desired output in the attachment
file.
I''ve made just 6 columns in it, but in reality I have over than 20 columns.

I hope that it it's explicit anough.

Hmmm, looks like your columns are quite regular:

 - a 10-column ID of some sort;
 - a 10-column First Name(?);
 - a 10-column Last Name(?);
 - a 10-column numeric code;
 - a 3-column alphabet code; and
 - a 10-column date.

A Perl one-liner should do the trick:

 $ perl -pi.bak
-e'chomp;@a=unpack("a10a10a10a10a3a10",$_);$_=join(":",@a)."\n"'
<input-file>

Which exactly translates to the following script:

#!/usr/bin/perl -i.bak

LINE:
while (<>) {
 chomp;
 @a = unpack("a10 a10 a10 a10 a3 a10", $_);
 $_ = join(":" => @a) . "\n";
} continue { print or die "-p destination: $!\n" }

Of vital note here is the unpack() function, which does the work of
splitting a record line into distinct fields to be placed in an array,
which is then joined later with colon glue.  Hence, if your real input
file contains 20 or so columns, you will have to adjust the template
string (those "a10"s up there) accordingly; just look at the above for
hints, or see the good docs on the pack() function in perlfunc.

Btw, you can run that one-liner safely on the input file directly,
since that incantation makes a backup copy with the same filename,
just appended with a `.bak'.

Hope that helps! =)

Cheers,

Zakame

--
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

Reply via email to