read space filled file and replace text at specific position

Hi I have a spaced filled file having records like below:

What I want is to read line having RT3 at position 17-19 then go to position 2651 check the 18 characters (might be space filled till 18 characters). This position should have a decimal number, if it has whole number then replace this number with number in a variable say "chrg" and write to another file.

When we read this space filled file the read command truncates the spaces, so we cannot find the contents in specific position. As we want to write back after replacing the number, the new file should be same as pervious file (space filled) so that it can be used by another program.

2791U00218110408RT31007801870070102120320081203200800000000|||||||||||1603661||||||||||||||||||||||||||||||516636|||||||||||||39707||||||||||||476929||||||||||||||||||||||||||||||||2022|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||0||||||||||||||||||||||||||||||||||||||||||||||||478951|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||0||||||||||||478951||||||||||||||||||||||||||||||||||||||||||||||||478951||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||2006||||||||||||398907|||||||||||||30.00|||||||||119672.10||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||2007|||||||||||||80044|||||||||||||30.00||||||||||24013.20|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||143685.30||||||||||27652.25||||||||||||||||||||||||||||||||0.00|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||116033.05|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||116033.05|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||116033.05|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||116033.05|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||10048|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||41812|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||0|||||||||||||||||X|||||||||||||||||||||||||||116033.05||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||478951||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

where | here has repalced spaces for more clarification.

The position 2651 here is shown in bold as we want to replace this string with some other variable but only for RT3 records. There are other records also but they dont have RT3 string in them. The file content shown above is one big whole string.

Please advise.

You can try something like that (not tested) :

awk -v new="$chrg" '
substr($0, 17, 3) == "RT3" {
   if (substr($0, 2651, 18) ~ /^[0-9]* *$/) 
      printf("%s%-18.18s%s\n", substr($0, 1, 2650), new, substr($0, 2669));
   next;
}
1
' inputfile

Jean-Pierre.

Can you explain the logic as I am new to AWK

awk -v new="$chrg" '
. . .

' input file

Run AWK, the executed program in ' . . .', the input file is inputfile and the variable new is defined.

substr($0, 17, 3) == "RT3" {

. . . .
}

Select lines with RT3 in pos 17 (length 3), and execute { . . . . }

if (substr($0, 2651, 18) ~ /^[0-9]* *$/) 
   printf("%s%-18.18s%s\n", substr($0, 1, 2650), new, substr($0, 2669));

If the record contains digits and spaces at position 2651 (length 18), print
the record with value of variable new in position 2651 (length 18).

next

Proceed next record

1

Print record (records without RT3)

jean-Pierre.

Thanks for the explanation, I have tested the script and there is problem that it replaces the previous string values with spaces and the position is written by the new variable.

This is the output

2791U00218110408RT31010105114190102151120071911200709042008|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||166031.25||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Can you try with another version of awk? Do you have nawk, or mawk, or gawk?

Or try Perl:

perl -pe 'if (substr($_,16,3) eq "RT3" && substr($_,2560,18) =~ /^\d+ *$/) {
  substr($_, 2560, 18) = sprintf("%-18i", $ENV{"chrg"});
}' file

... assuming chrg is an integer. Or simply use the %-18.18s format suggested by aigles.

If this doesn't work for you, either, maybe you could attach a small sample file to test with. The sample you posted only has spaces at position 2651 in the version I copy+pasted from here. The number 478951 is at position 2690 in my copy. (In Perl, these numbers are one less, because substr counts offsets from zero, not positions.)

hi,
I am not sure whether understood your requirement totally and exactlly.

Anyway, hope below one can help you a little.

file:

123RT3kljfsd12.3saf
234sddksadfjLEOkjffs
234RT3fjsdka1234fkdjs

output:

input 23.4
123RT3kljfsd12.3saf
234sddksadfjLEOkjffs
234RT3fjsdka23.4fkdjs

code:

read t
cat file | nawk -v tt="$t" '{
if(substr($0,4,3)=="RT3")
{
	temp=substr($0,13,4)
	if (temp+0==temp && index(temp,".")==0)
		printf("%s%s%s\n",substr($0,1,12),tt,substr($0,17))
	else
		print
}
else
	print
}'