Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
How to insert a character after the first byte in a line in Linux

I have a setting file states 1,2,3 meaning to place a , after the 1st, 2nd, and 3rd byte.

For example my input is

stackoverflow

then I want to make it

s,t,a,ckoverflow

How should I do it with Linux tools? I feel sed should be used, but I don't know how to do it.

Edit:

I have more than 10 commas to be placed. And the line also includes multi-byte characters.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

In sed:

echo stackoverflow | sed 's/./&,/1;s/./&,/3;s/./&,/5'

s/.../.../n replaces the nth match. After the first replacement, the second byte would now be the third match, and after that, the third byte would now be the fifth.

Or, with regex groups:

echo stackoverflow | sed 's/\(.\)\(.\)\(.\)/\1,\2,\3,/'

In Basic Regular Expressions (BRE), \(…\) is used to group expressions. You can refer to the text that matched the nth group using \n, to re-use matched text in the replacement, for example. So, in this case, I have three groups, each containing just ..

With GNU sed, you can avoid the \ by using Extended Regular Expressions (ERE):

echo stackoverflow | sed -r 's/(.)(.)(.)/\1,\2,\3,/'

With multibyte charsets, try with the C locale:

$ echo 'æ  ' | LC_ALL=C sed 's/\(.\)\(.\)\(.\)/\1,\2,\3,/'
�,�, , 
$ echo → | LC_ALL=C sed 's/\(.\)\(.\)\(.\)/\1,\2,\3,/'
�,�,�,
over 4 years ago · Santiago Trujillo Denunciar

0

IFS=, read -ra arr <<< "$1"
rules=()
for i in "${arr[@]}"; do
    rules[$i]=1
done

s=stackoverflow
for (( i=0; i<${#s}; i++ )); do
    if (( ${rules[i+1]} )); then
        printf '%s,' "${s:i:1}"
    else
        printf '%s' "${s:i:1}"
    fi
done

Usage example:

$ ./sof 1,2,3
$ s,t,a,ckoverflow
$ ./sof 1,2,3,7
$ s,t,a,ckov,erflow
over 4 years ago · Santiago Trujillo Denunciar

0

This might work for you (GNU sed):

sed 's/\B/\n/g;s/\n//4g;s/\n/,/g' file

or:

sed 's/\B/,/;s/\B/,/;s/\B/,/' file
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda