Tyler Tyler - 5 months ago 9
Bash Question

Separate huge Hash file into one file based on content (shell scripting)

I have a huge file of hashes that is displayed like this:

username:values:values:password hash:::

username:values:values:password hash:::

username:values:values:password hash:::


and so on....

I need to take the hashes with this format for username and put them into another file.

the username needs to start with a letter followed by 6 digits.

Examples of usernames i want to put into a new file:
Y783071:SDF87DSFQ1:DSF98D3EF:DSF98SDF98
G678876:SD8F7SD8F7:DF98SDF89:SDF98S
J110998:SDF98FJ98W:9J493H0L8:3498RJHT


However there are a ton of other usernames in this hash file that I do not want that may look like this:
SDFSNPOSAB:9786:sdfb787
EMEU6798:JHB98fds:

etc etc

Preferably in bash or C++.

I was told I should use grep or sed

I'm on a ubuntu linux machine.

Answer

You can use below sed command:

sed -n '/^[a-zA-Z]\{1\}[0-9]\{6\}:/p' hash_file > output_file

This sed command looks for lines starting with 1 alphabet ( [a-zA-Z]\{1\} ) followed by 6 numbers ( [0-9]\{6\}) followed by :.

Comments