How to use sed to replace multiple numbered characters per line?

  Kiến thức lập trình

I know how to modify one character per line with a new character, here the 3rd character is being replaced in every line:

sed 's/^(.{2})./1;/' text.txt > textm.txt

Now I need to replace multiple characters per line. Lets say 5th, 10th, 23rd e.t.c with the same new character which is semi colon in this case. How is that supposed to be done?

I tried below but it does not work:

$ sed 's/^(.{5,10,23})./1;/' text.txt > textm.txt
sed: -e expression #1, char 25: Invalid content of {}

1

This might work for you (GNU sed):

sed 's/./;/5;s//;/10;s//;/23' file

Replace the 5th, 10th and 23rd characters with ;.

N.B. When the LHS of the substitute command is empty it is replaced by the previous regex. Also, it is safer to work from back to front so that the character(s) being replaced remain unaltered by the previous substitutions i.e.

sed 's/./;/23;s//;/10;s//;/5' file

2

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT