Thursday, January 27, 2011

Inserting characters in the middle of a string

Recently I had a problem to solve that involved inserting a byte in the middle of a line, and it was 522K lines :(.

Sometime ago I saw a video from Aurélio that teaches how to accomplish that: http://vimeo.com/11744241

But you don't have to watch the video (its in portuguese). The simple way is using sed:

We will seach for anything ('s), change this anything to itself plus a new byte ('s/./&39) and say on which position ('s/./&39 /3):


$ echo "#####" |sed 's/./&39 /3'
###39 ##


With almost the same code, we could change the 3rd character to something else:


$ echo "#####" |sed 's/./3/3'
##3##

No comments:

Post a Comment