Insert a character at a specific location using sed
问题 How to insert a dash into a string using sed (e.g., 201107 to 2011-07 )? 回答1: echo "201107" | sed 's/201107/2011-07/' Should work. But just kidding, here's the more general solution: echo "201107" | sed 's/^\(.\{4\}\)/\1-/' This will insert a - after the first four characters. HTH 回答2: For uncertain sed version (at least my GNU sed 4.2.2), you could just do: echo "201107" | sed 's/./&-/4' /4 -> replace for the 4th occurrence (of search pattern . ) & -> back reference to the whole match I