How would I replace the 9th character of every line in a file?

Is there a simple way to replace the 9th character of every line in a text file?

Right now it is a space and instead I want to stick in a 0, every line is the same.

Is there a quick way to do this in Unix? (Solaris)

Try using sed...

sed 's/\(^........\) /\10/' file1 > file2

Or awk...

awk '{print substr($0,1,8) "0" substr($0,10)}' file1 > file2

Worked perfect, thanks.

Can you explain how it works though?

awk '{print substr($0,1,8) "0" substr($0,10)}' file1 > file2

I understand the awk part, the "0" part, and the file1 > file2 part but the substr part.. I see that it works but not why it does.

{print substr($0,1,8) "0" substr($0,10)}

Awk's concatenation is built into the language. Awk is concatenating the first part of the record (positions 1 through 8) then setting the 9th position to "0". Lastly, the substring command is used to concatenate the remaining portion of the record (from position 10 to the end of the file) onto the new record which is output in the print statement.

Wow.

That's a pretty clever way of doing it. I doubt I would have thought about that angle, what I was thinking was a specific character replace.

Neat.

Thanks.

Or even simpler

sed 's/./0/9' file1 > file2

Which'll replace the 9th occurrence of "any character" with a zero.

Cheers
ZB