Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 912885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:27:39+00:00 2026-05-15T17:27:39+00:00

This IS kind of linked to another question – Code golf: "Color highlighting" of

  • 0

This IS kind of linked to another question – Code golf: "Color highlighting" of repeated text

I’m tying to figure out a way of breaking a file into all ‘n’ characters long groups.

Eg: If a file comprises of ONLY the following text:

ABCDEFGHIJ

And we want it broken into sets of 3, the output should be:

ABC
BCD
CDE
DEF
EFG
FGH
GHI
HIJ

No characters in the file are to be treated any differently from another. ie, a “space” is just another character which should follow the rule above…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:27:40+00:00Added an answer on May 15, 2026 at 5:27 pm

    sed:

    echo "ABCDEFGHIJ" | sed -n ':a;/^...$/{p;b};s/.../&\n/;P;s/.//;s/\n//;ba'
    

    A more generalized sed version:

    num=5; echo "ABCDEFGHIJ" | sed -n ":a;/^.\{$num\}\$/{p;b};s/.\{$num\}/&\n/;P;s/.//;s/\n//;ba"
    

    Bash and ksh:

    string="ABCDEFGHIJ"
    for ((i=0;i<=${#string}-3;i++)); do echo ${string:i:3}; done
    

    zsh:

    string="ABCDEFGHIJ"
    for ((i=1;i<=${#string}-2;i++)); do echo $string[i,i+2]; done
    

    sh (specifically Dash):

    string='ABCDEFGHIJ'
    count=$(seq $((${#string}-2)))
    for i in $count; do b="$b?"; done
    for i in $count; do b="${b%?}"; echo "${string%$b}"; string="${string#?}"; done
    

    AWK:

    echo "ABCDEFGHIJ" | awk -v num=4 '{for (i=1; i<length($0)-num;i++) print substr($0,i,num)}'
    

    Edit: Added a more generalized sed version and an AWK version.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this kind of follows on from another question of mine. Basically, once I have
I have ask this kind of question before, but it seems my previous question
I have this kind of code in some applications (from microsoft) [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage( Microsoft.Naming,
OK, this kind of follows on from my previous question . What I would
I know questions of this kind have been asked before, but my situation differs
My stomach churns when I see this kind of output. http://www.freeimagehosting.net/uploads/e1097a5a10.jpg and this was
I have a file that contains this kind of paths: C:\bad\foo.c C:\good\foo.c C:\good\bar\foo.c C:\good\bar\[variable
Here's a coding problem for those that like this kind of thing. Let's see
I would like to make a simple desktop application to learn this kind of
I'm implementing a table with a fixed header using this kind of setup: <div

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.