Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

297
Views
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 answers
Answer question

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 Report

0

I think

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

should do it.

over 4 years ago · Santiago Trujillo Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!