Replace characters at fixed positions

My objective is to replace the 8th, 9th, 10th characters by 1 space per character (total 3 spaces) in a file.

I achieved this using following command:

sed 's/\(.\)/\1@/7;s/@\(...\)/   /' FileData.txt > FileData_UPDATED.txt

Another situation comes when I need to done same but excluding 1st and last lines.
I tried using following but it removed the first and last line in the output:

sed '1d;$d' FileData.txt | sed 's/\(.\)/\1@/7;s/@\(...\)/   /' > FileData_UPDATED.txt

Please suggest how the first command can be updated to exclude the first and last lines.

Hi, try:

sed '1n;$n;s/./&@/7;s/@.../   /' FileData.txt
2 Likes

Hi,

Thank you very much for your response. It worked like the way I wanted. Thanks.

Is there also a way if I can skip changing the lines starting with "EDI_DC40" OR "E2EDS01" instead of 1st and last lines?

Try (untested)

sed '/EDI_DC40/n;/E2EDS01/n; ...'

or, with ERE,

sed -E '/EDI_DC40|E2EDS01/n; ...'

Thank you.

sed '/EDI_DC40/n;/E2EDS01/n; ...'

Getting error: "Unrecognized command: /EDI_DC40/n;/E2EDS01/n; ..."

sed -E '/EDI_DC40|E2EDS01/n; ...'

Getting error: "sed: illegal option -- E"

I am using sun-sparc. Not sure, if its due to platform difference.

There must some sort of typo, perhaps you forgot the opening quote? The three dots were not meant literally by RudiC. Try:

sed '/EDI_DC40/n;/E2EDS01/n;s/./&@/7;s/@.../   /' FileData.txt