the way I am trying is
1)Read the file Layout.txt line by line
2)Get position of ( , ) ,:
3)set the position of start and end position
4)read abc.txt and append the file with ~ at above given location.
However I got stuck in step 3 which is as below , variable in index gives error.
also Is there any better and efficient method to do it ?
#!/usr/bin/ksh
input="./Emi11LayoutAsDefinedInCLEC.TXT"
start_character="("
end_character=")"
tmp=":"
function getPosition
{
echo "getPosition : $var"
S=`echo $var |awk '{print index($0,$start_character)}'`
E=`echo $var |awk '{print index($0,$end_character)}'`
echo "START POSITION of $start_position --------------------->: " $S
echo "END POSITION of $end_position --------------------->: " $E
}
count=0
while IFS= read -r var
do
echo "Reading : Starts **********"
echo $var
getPosition $var
echo "Reading : Finish **********"
((count=$count+1))
done < "$input"
CATEGORY POSITION (1:2) char, 2
GROUP_ID POSITION (3:4) char, 2
DOR_MONTH POSITION (9:10) char, 2
About layout means position
-from 1 to 2 is Category and hence we need separator
-from 3 to 4 is GROUP_ID and hence we need separator
-from 9 to 10 is DOR_MONTH and hence we need separator
Final output
--------------
my $sourceFile="/v/global/user/b/bh/bharade/Programs/Forum_Scripts/Layout.txt";
my $targetFile="/v/global/user/b/bh/bharade/Programs/Forum_Scripts/abc.txt";
open(FH1,"$sourceFile") or die "$!\n";
open(FH2,"$targetFile") or die "$!\n";
my $i=1;
while (<FH1>) {
if (/(\d+)\:(\d+)/) {
my $start=$1;
my $end=$2;
if ($. == 1) {
if ( $start != 1) {
my $newEnd=$start - 1;
my $newStart=0;
$hash{$i}=[$newStart,$newEnd];
++$i;
}
}
$hash{$i}=[$start - 1,$end];
++$i;
}
}
my $prevEnd=0;
while (<FH2>) {
foreach ($j=1;$j<$i;$j++){
if ( ($hash{$j}->[0] - $prevEnd ) > 1 ) {
$gapstart=$hash{$j}->[0] - $prevEnd;
$gapEnd=$hash{$j}->[0] - 1;
print substr ($_,$gapstart,$gapstart),"~";
}
print substr ($_,$hash{$j}->[0],$hash{$j}->[1] - $hash{$j}->[0] ),"~";
$prevEnd = $hash{$j}->[1];
}
if (length($_) != $prevEnd ) {
print substr ($_,$prevEnd);
}
}
close (FH1);
close (FH2);
while IFS=" (:)" read _ _ P _ _ L; do ((IX++)); POS[$IX]=$((P-1)); LEN[$IX]=$L; done < layout.txt
while read T
do unset CRS L
for i in ${!POS[@]}
do L=$L${T:$CRS:$((${POS[$i]}-CRS))}"~"${T:${POS[$i]}:${LEN[$i]}}"~"
CRS=$((${POS[$i]}+${LEN[$i]}))
done
L=${L#\~}
L=${L//~~/\~}
L=$L${T:$CRS}
printf "%s\n" $L
done < abc.txt
11~01~0116~03~15100000000000000
11~01~0116~03~15100000000000000