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

327
Vistas
Bash regexp: How to replace all non-whitespace characters with a letter?

I have a text file that contains a lot of whitespace and some other ASCII characters. I would like to use Bash (preferably via sed) to replace any non-whitespace characters in the file with the letter M. How would I go about doing this?

Thanks for helping.

edit: For the downvoters, I tried running this:

sed -i 's/[^\s]/M/g' file

and this:

sed -i 's/[^\s]/M/' file

but neither of them worked out too well. I'm a little unfamiliar with regex, so apologies if I'm doing something obviously wrong.

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

0

You could use POSIX character classes:

sed -i 's/[^[:space:]]/M/g' file

For example:

$ echo 'a b       c' | od -a                  
0000000   a  sp   b  sp  ht   c  nl
0000007
$ echo 'a b       c' | sed 's/[^[:space:]]/M/g' | od -a
0000000   M  sp   M  sp  ht   M  nl
0000007

Note:

  • The g flag to the s command, since you want to replace all matches in a line
over 4 years ago · Santiago Trujillo Denunciar

0

Use tr

tr -c '[:space:]' M < 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