0

I have a file with alphabetically sorted lines. The file starts from 0-9 till a-z. Example

000dasd
001dss
awer
addd
alko
babb
bobb
...
zzasd

Since the file is really big I want to divide it into multiple files where in each file I will store different blocks - one file that will contain all lines starting with 0-9, one file containing [a-d], one file containing [e-h], and so on.

Currently, I have a grep command which does this but for just one char. Since I'm really new in Linux, grep, awk, and shell scripting I can use some help here.

That's how I do it for all lines that are starting with 'a'

grep "^[[:blank:]]*a" file.txt > a-file.txt

1 Answer 1

0

You can use [] to enclose a range of characters so [0-9] would match all arabic digits, [a-d] would match the letters a, b, c and d etc.

So you could run

grep "^[0-9]" file.txt > digit-file.txt
grep "^[a-d]" file.txt > a-d-file.txt

etc

1
  • Works perfectly. Thanks!
    – Jake
    Oct 24, 2022 at 10:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .