insert pipe in file to separate values

hi all...
i need your help, because i don�t know what to do...
i have a flat file like this:

B065200512312004123111010000061451 000021853
B065200512312004123111020000621907 000417802
B065200512312004123111030000005214 000005861
B065200512312004123111040000120133 000088448

and i need insert the pipe character (" | ") in each record, to separate the values. I need this:

B|065|20051231|20041231|11010|000061451| |000021853|
B|065|20051231|20041231|11020|000621907| |000417802|-
B|065|20051231|20041231|11030|000005214|-|000005861|
B|065|20051231|20041231|11040|000120133| |000088448|-

how can i do that? is that possible?
please any idea, thanks...

You could use Perl's unpack() to read fixed length field records.

e.g. put something like this in a file and have it run through Perl

<pre>
$ cat insert_pipes.pl
#!/usr/bin/perl

while (<DATA>) {
chomp;
@unpacked = unpack 'A1A3A8A8A5A9x1A*', $_;
print join('|', @unpacked[0..5], q{ }, $unpacked[-1], q{}), "\n";
}
__DATA__
B065200512312004123111010000061451 000021853
B065200512312004123111020000621907 000417802
B065200512312004123111030000005214 000005861
B065200512312004123111040000120133 000088448

$ ./insert_pipes.pl
B|065|20051231|20041231|11010|000061451| |000021853|
B|065|20051231|20041231|11020|000621907| |000417802|
B|065|20051231|20041231|11030|000005214| |000005861|
B|065|20051231|20041231|11040|000120133| |000088448|
</pre>

N.B. to moderators,
How do I enable markup recognition in this forum?

use html code tags [ code ] [ / code ] without spaces

is there a way using ksh to doing this?
because i don't know how to use perl... :confused:

thanks

Certainly you can also do this with shell commands.
I am convinced someone will come up with a solution soon.
You could for instance do some character counting and use
the cut command with the -c flag and appropriate column numbers
that correspond to the positions where a pipe delimiter should follow.
(man cut).
If you are using Bash or similar Posix shell you could also use the substring
variable expansion (man bash).
e.g.
(not pretty elegant, you shouldn't use a here file but redirect input from your data file)

while read line; do
printf "%c|%s|%s|%s|%s|%s| |%s|\n" \
        ${line:0:1} ${line:1:3} ${line:4:8} ${line:12:8} \
        ${line:20:5} ${line:25:9} ${line:34:9}
done <<DATA
B065200512312004123111010000061451 000021853
B065200512312004123111020000621907 000417802
B065200512312004123111030000005214 000005861
B065200512312004123111040000120133 000088448
DATA

if you have gawk - you can use gawk's 'FIELDWIDTH' to break up fields.

if you don't have gawk....... you can use 'nawk' iwth the function below to simulate gawk's functionality. Here's the link to USENET's comp.lang.awk original posting

nawk -f debian.awk myFile.txt

debian.awk:

function setFieldsByWidth(   i,n,FWS,start,copyd0) {
  # Licensed under GPL Peter S Tillier, 2003
  # NB corrupts $0
  copyd0 = $0                             # make copy of $0 to work on
  if (length(FIELDWIDTHS) == 0) {
    print "You need to set the width of the fields that you require" > "/dev/stderr"
    print "in the variable FIELDWIDTHS (NB: Upper case!)" > "/dev/stderr"
    exit(1)
  }

  if (!match(FIELDWIDTHS,/^[0-9 ]+$/)) {
    print "The variable FIELDWIDTHS must contain digits, separated" > "/dev/stderr"
    print "by spaces." > "/dev/stderr"
    exit(1)
  }

  n = split(FIELDWIDTHS,FWS)

  if (n == 1) {
    print "Warning: FIELDWIDTHS contains only one field width." > "/dev/stderr"
    print "Attempting to continue." > "/dev/stderr"
  }

  start = 1
  for (i=1; i <= n; i++) {
    $i = substr(copyd0,start,FWS)
    start = start + FWS
  }
}

#I then call setFieldsByWidth() in my main awk code as follows:

BEGIN {
  #FIELDWIDTHS="7 6 5 4 3 2 1" # for example
  FIELDWIDTHS="1 3 8 8 5 9 1 9" # based on fieldwidth formating taken from sample - vlad
  OFS="|"
}
!/^[  ]*$/ {
  saveDollarZero = $0 # if you want it later
  setFieldsByWidth()
  # now we can manipulate $0, NF and $1 .. $NF as we wish
  print $0 OFS
  next
}