Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

330
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

Use tr

tr -c '[:space:]' M < file
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda