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

299
Vistas
Replace a line starting with '-' hyphen with a character repeated n times

I have a text file that has some lines like this (hyphen repeated)

-------------------------------------------------------

I need to replace these lines with character 'B' repeated 1500 times. For example, like

BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB

Any suggestions using 'sed' or 'awk' command?

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

0

With awk:

$ awk '/^-+$/ {s = sprintf("% 1500s", ""); gsub(/ /,"B",s); print s; next} 1' file

Or, maybe a bit more efficient if you have many such lines:

$ awk 'BEGIN {s = sprintf("% 1500s", ""); gsub(/ /,"B",s)} \
       /^-+$/ {print s; next} 1' file
over 4 years ago · Santiago Trujillo Denunciar

0

I think

perl -pe 'my $bb = "B"x1500; s/^-+$/$bb/g'

should do it.

over 4 years ago · Santiago Trujillo Denunciar

0

printf+sed variant:

$ cat file
1111
----
2222

$ sed -r 's/^-+$/'"$(printf --  "%.1s" B{1..150})/" file
1111
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
2222

Here sed is used only for replacement.
printf is used for generating 1500 times B. (The above scriptlet has 150 instead of 1500, because it required too much scrolling.)

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