I have 14-character line containing digits. How do I insert a char into it at the specific location, i.e. at 4th? So, if I have string like this: xxxxxxxxxxxxxx how do I change it to something like this: xxxx-xx-xxxxxxxx ? (x = digit)
Thanks! irek
If your lines only contain your digits, you can group the first four characters in a group:
\(....\)
and the following two ones in another group:
\(....\)\(..\)
Then, you just replace it by a backreference to the first group (\1
), a dash, a backreference to the second group (\2
) and another dash:
\1-\2-
The result:
$ echo 12345678900000 | sed 's/\(....\)\(..\)/\1-\2-/'
1234-56-78900000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With