Operating System - HP-UX
1833780 Members
2749 Online
110063 Solutions
New Discussion

how to replace a character in a file

 
SOLVED
Go to solution
Stefano_10
Advisor

how to replace a character in a file

I have to replace all the character in a file at a fixed column position (ex: all the 11th characters of all lines) with a specific character.
How can i do this?

Thanks
Stefano
5 REPLIES 5
Robin Wakefield
Honored Contributor
Solution

Re: how to replace a character in a file

Hi Stefano,

sed 's/./a/11' filename

will change the 11th character of each line to "a".

rgds, Robin
H.Merijn Brand (procura
Honored Contributor

Re: how to replace a character in a file

# perl -pe'11
replaces every x with y on the 11th position in the line of file

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Steve Steel
Honored Contributor

Re: how to replace a character in a file

hi

cat file|while read line
do
echo $(echo $line|cut -c1-10)"?"$(echo $line|cut -c12-128)
done > newfile


Where ? is the new character


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
H.Merijn Brand (procura
Honored Contributor

Re: how to replace a character in a file

# perl -pe'11
replaces every x with y on the 11th position in the line of file

# perl -pe'11
replaces every 11th character on each line with "y"

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Sergejs Svitnevs
Honored Contributor

Re: how to replace a character in a file

The vi editor.

To replace any character at position 11 with an "A":

:%s/^\(.\{11\}\)./\1A/

Regards,
Sergejs