Replace nth position character of all the lines in file

I want to replace 150th character of all the lines in a file using sed or awk...
searched the forums but didn't find exact answer

replace the 150th character as X

awk '$0=substr($0,1,149)"X"substr($0,151,length($0))' input.txt > output.txt

---------- Post updated at 12:10 PM ---------- Previous update was at 12:07 PM ----------

if you have the lines with less than 150 characters, and dont want to append the X in the end, then use the below

 
awk 'length($0)>150{$0=substr($0,1,149)"X"substr($0,151,length($0))}1' input.txt

use nawk in solaris

With sed:

sed 's/^\(.\{149\}\)./\1X/' inputfile
1 Like

Shorter:

sed 's/./X/149' input

To replace the 150th character, that should be 150, not 149.

Regards,
Alister

Ah, my bad. You're right alister.

On the same lines how do I replace nth column in the file ?

for example

assgin  crclk  unit1 unit2
assign tap  signal_a

Want to put = after 2nd column

assign crclk = unit1 unit2
assgin tap = signal_a

You may wish to re-post your post with accurate spacing between the fields and the before/after data posted in Code Tags?

try this

awk '{$3=" = "$3;print}' filename

output is

assgin crclk  = unit1 unit2
assign tap  = signal_a

try this one :

awk -F "" '{gsub(".","Y",$150)}1' OFS="" infile

n for column :

$ awk '{gsub($2,$2"=",$2)}1' infile