Replacing a position in a file

Hi
I'm trying to use awk in a file(test123.dat).
My requirement is to to check for the 65th position, if the 65th position is a space then replace the 65th position by the number 9.

This is the code that i used:
awk '{substr($0,65,1) ~ / / }{sub(substr($0,65,1),"9")}{print}' test123.dat

But this doesnt work :confused:
Plz Help...

A little change...

awk '{if(substr($0,65,1)==" ")sub(substr($0,65,1),"9");print}'

-----Post Update-----

by the way
this was wrong

 (substr($0,5,1) ~ / / ) 

-----Post Update-----

by the way
this was wrong

 (substr($0,5,1) ~ / / ) 

This is a sample data from the file test123.dat

0049DA06152009FPA06USD 000000000000D000000000009.9

If i use awk '{if(substr($0,65,1)==" ")sub(substr($0,65,1),"9");print}' test123.dat

this is the result that i get, the "9" gets appended after USD

0049DA06152009FPA06USD9 000000000000D000000000009.9

but i should get the result

0049DA06152009FPA06USD 000000000000D000000000009.99

 
 
echo "0049DA06152009FPA06USD 000000000000D000000000009.9" | awk '{ print length($0) }'
50

r you trying to modifi 65th character ?..

I had pasted only a part of the data in my previous post, sorry abt it

This is the actual data in the file

0049DA06152009FPA06USD 000000000000D000000000009.9 ORA100144

The total length is 149 and im trying to do the changes in the 65th position

When i post the actual data the spaces/tabs in the actual data does not get posted.

try something like this

 
 echo "0049DA06152009FPA06USD 000000000000D000000000009.9 ORA100144" |
awk 'substr($0,51,1) ~/[^0-9A-Z]/ {print substr($0,1,50)"9"substr($0,52)}'

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

Got it :slight_smile:
Thanks a lot!!!!