I have a set of my user database dumps in .txt files (let's assume in the C:\Dumps folder)
The structure of all .txt files is identical:
login:password
login:password
login:password
My question is what is the easiest way to separate logins from all files to "logins.txt" and separate passwords for "passwords.txt"? These are data for statistical purposes, so no order [ultimately will be sorted anyway]
I tried :
gawk.exe -F: "{print $ 1}" dump1.txt > logins1.txt
gawk.exe -F: "{print $ 2}" dump1.txt > passwords1.txt
gawk.exe -F: "{print $ 1}" dump2.txt > logins2.txt
gawk.exe -F: "{print $ 2}" dump2.txt > passwords2.txt
and the method is effective, but due to the number of files, I wanted to ask about a simple method to automatically separate these two data from the files that I have in this folder.
You can run awk on multiple files and also action redirection from within awk code and so:
awk -F: '{ split(FILENAME,map,".");fileno=substr(map[1],length(map[1]));print $1 > "login"fileno".txt"; print $2 > "password"fileno".txt" }' dump*.txt
We set the field delimiter to : and then use awk's reserved FILENAME variable to track the existing file being processed. With this we get the file name prefix by splitting the filename in an array map, using awk's split function and referencing map[1]. We then further process this with awk's substr function to get the file number from the last character of the file prefix. We utilise this filno variable to output $1 to the login file and $2 to the password file.