I have hundreds of *.txt
files which have a common format.
I can insert a comma at a specific position in one file, how can I generalize the below code to apply this operation at several places for all *.txt
files in the directory?
sed -i 's/^\(.\{4\}\)/\1,/' blank.txt
For example inserting commas at positions 4, 8, 22 etc.
Something like this perhaps?
for i in *.txt; do
sed -i 's/^\(.\{4\}\)/\1,/' $i
done
Best Answer
Start from the rightmost one:
Otherwise, the first substitution would affect the offset for the second. You can always account for it though: