read space filled file and replace text at specific position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting read space filled file and replace text at specific position
# 1  
Old 04-17-2008
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.

Code:
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.
# 2  
Old 04-17-2008
You can try something like that (not tested) :
Code:
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.
# 3  
Old 04-17-2008
Can you explain the logic as I am new to AWK
# 4  
Old 04-17-2008
awk -v new="$chrg" '
. . .
Code:
' input file

Run AWK, the executed program in ' . . .', the input file is inputfile and the variable new is defined.
Code:
substr($0, 17, 3) == "RT3" {


. . . .
}


Select lines with RT3 in pos 17 (length 3), and execute { . . . . }
Code:
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.
# 5  
Old 04-17-2008
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
Code:
2791U00218110408RT31010105114190102151120071911200709042008|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||166031.25||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

# 6  
Old 04-20-2008
Can you try with another version of awk? Do you have nawk, or mawk, or gawk?

Or try Perl:

Code:
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.)
# 7  
Old 04-21-2008
hi,
I am not sure whether understood your requirement totally and exactlly.

Anyway, hope below one can help you a little.

file:
Code:
123RT3kljfsd12.3saf
234sddksadfjLEOkjffs
234RT3fjsdka1234fkdjs

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

code:
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
}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Search for a pattern and replace a space at specific position with a Character in File

In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123. So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Discussion started by: Jagmeet Singh
3 Replies

2. Shell Programming and Scripting

How to read data from tab delimited file after a specific position?

Hi Experts, I have a tab deliminated file as below myfile.txt Local Group Memberships *Administrators *Guests I need data in below format starting from 4th position. myfile1.txt Administrators Guests the above one is just an example and there could... (15 Replies)
Discussion started by: Litu1988
15 Replies

3. Shell Programming and Scripting

Find and replace with 0 for characters in a specific position

Need command for position based replace: I need a command to replace with 0 for characters in the positions 11 to 20 to all the lines starts with 6 in a file. For example the file ABC.txt has: abcdefghijklmnopqrstuvwxyz 6abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz... (4 Replies)
Discussion started by: thangabalu
4 Replies

4. Shell Programming and Scripting

position specific replace in file

How to replace the position specific values in the file.. i searched a lot the forums but i couldn't able to do... i have file like below 576666666666666666666666666 7878 897987 121 0asdas Y12 5900fbb 777 09JJJ 78798347892374 234234234364 234232898 89HJHIHIGIUG989902743748327khjkhkjlh... (6 Replies)
Discussion started by: greenworld123
6 Replies

5. Shell Programming and Scripting

Using sed to replace a string in a specific position

I asked this before, but my problem got more complicated. Heres what I am trying to do: I'm trying to replace a string at a certain location with another string. Heres the file I'm trying to change: \E I want to replace the escape code at the 3rd line, 2nd column with this escape code... (3 Replies)
Discussion started by: tinman47
3 Replies

6. UNIX for Dummies Questions & Answers

How do you view specific columns from a space delimited text file?

I have a space delimited text file with 1,000,000+ columns? I would only like to view specific ones (let's say through 1:10), how can I do that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

7. Shell Programming and Scripting

remove white space from specific columns in text file

Hello i have a text file like this: 1 AB AC AD EE 2 WE TR YT WW 3 AS UY RF YT the file is bigger , but that's an example of the data what i want to do is to merge all columns together except the first one, it will become like this : 1 ABACADEE 2 WETRYTWW 3 ASUYRFYT (8 Replies)
Discussion started by: shelladdict
8 Replies

8. Shell Programming and Scripting

Sed position specific replace

I'm drawing a blank on how to use sed to replace selectively based on position in the string (vs nth occurence): hello.|there.|how.|are.|you.| I want the period removed in the 3rd item (as defined by the pipe delimiter) if a period is present. So the result in this case would be: ... (2 Replies)
Discussion started by: tiggyboo
2 Replies

9. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies

10. UNIX for Dummies Questions & Answers

search and replace a specific text in text file?

I have a text file with following content (3 lines) filename : output.txt first line:12/12/2008 second line:12/12/2008 third line:Y I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before. I tried using cat output.txt... (4 Replies)
Discussion started by: santosham
4 Replies
Login or Register to Ask a Question