Score:0

Extract [a-c] from file and save in another file

pa flag

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
Score:0
us flag

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

Jake avatar
pa flag
Works perfectly. Thanks!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.