Pad space at the end of string and reformat

I need to read in the string from input file and reform it by cut each segment and check the last segement lenght. If the last segment length is not as expected (see below segment file or table. It is predefined), then pad enough space.

Old string

FU22222222CA6666666666AKxvbFMddreeadBP999

new string

FU22222222CA6666666666AKxvbFMddreead
BP999               &&

Segment table
id length
-----------------

FU 10
CA 12
AK 5
FM 9
BP 20
  1. Reform it on the basis of what?
  2. When you say "cut each segment and check the last segment length", what do you mean? How much of the string do you want to cut?

if you see the sample old and new strings I incluced, basically go through the entire string segment by segment (segment id and length are defined at the table at the end of post). Only if the last segment length is not as expected, then pad spaces, else the old string and new string should be the same,except that new string gets padded with && at the end of string.

In plain program language:
cut the first two byte from the input string as segment id, and lookup segment id table with segment id to get segment length. The cut the inputstring with that length and save the new cutted string. Repeat the same logic till the end of inputstring. If the length of last piece of string is the lenghth from segment lookup, then append space.

See the example, last segment BP is only 5 bytes long. But segment length for BP is 20 (see segment table), so the new string length for the BP will be 5+15 spaces.

Let me know if this is clear

Put your segment sizes in seg_table file:

FU 10
CA 12
AK 5
FM 9
BP 20

Then try this:

awk 'NR==FNR {W[$1]=$2;next}
{  start=width=0
   for(i=1; substr($0,i,2) in W; i+=W[substr($0,i,2)]) {
    start=i;width=W[substr($0,i,2)]; }
   if (start+width>length)
      printf "%s\n%*s&&\n", substr($0,1,start-1), -width, substr($0,start)
   else
       print $0"&&"
}' seg_table infile

Hi, Chubler XL,
this script works for one line, if file contains more than one line, it print out wrong result. for example: file like:

FU22222222CA6666666666AKxvbFMddreeadBP999
AK22222222CA6666666666CAxvx
CA12334555FU33333FM000 

seg code

 FU 10 
CA 12
AK 5 
FM 9 
BP 20

expected result:

FU22222222CA6666666666AKxvbFMddreead
BP999               &&
AK22222222CA6666666666
CAxvx     &&
CA12334555FU33333
FM000  &&

current script print out as following:

FU22222222CA6666666666AKxvbFMddreead
BP999               &&
AK22222222CA6666666666CAxvx&&
CA12334555FU33333FM000&&

Would you please take a look.

Thats how it is supposed to work, if format is wrong it just puts && on end of line:

   on 2nd line "AK" is 5 wide and "22" is undefined
   on 3rd line "CA" is 12 wide and "33" is undefined

This input file will work:

FU22222222CA6666666666AKxvbFMddreeadBP999
AK222CA6666666666CAxvx
CA124FU33311FM000

Yes, you are right,
Thanks

tested the code and it worked perfect. Thanks a lot.

One more question regarding this program. If I also need to remove the control characters from the string, can you help to add the additional logic

Thanks a lot

To remove control characters (before the field widths are counted) try this:

awk 'NR== FNR { W[$1]=$2 ; next}
{ start=width=0;
  gsub("[[:cntrl:]]", "")
  for(i=1; substr($0,i,2) in W; i+=W[substr($0,i,2)]) {
    start=i;width=W[substr($0,i,2)]; }
   print i, start, width , length, substr($0,i,2)
   if (start+width>length)
      printf "%s\n%*s&&\n", substr($0,1,start-1), -width, substr($0,start)
   else
      print substr($0,start+width,2) ":Undefined!"
}' seg_table infile

It works perfect. Do you know how to insert control character with VI editor? Also how can I send the output to a file? I tried and could not make it work

you may try below perl

my $str='FU22222222CA6666666666AKxvbFMddreeadBP99';

while(<DATA>){
    my @tmp = split;
    $hash{$tmp[0]}=$tmp[1]-length($tmp[0]);
    $reg2=$reg2.'|'.$tmp[0];
}
$reg2=~s/^\|//;
my @tmp =split(/(?=(?:$reg2))/,$str);
map { s/($reg2)(.*)/$1.$2.' 'x($hash{$1}-length($2))/e} @tmp;
print join "", @tmp;
print "<---\n";
__DATA__
FU 10
CA 12
AK 10
FM 9
BP 10

Thanks Summer Cherry for providing the perl script. As I do not have any perl experience, would you mind send me the complete script. So I can execute. Also can you please replace the string with file. the actual data I need to process is in a file.