1

I'd like to use Keepass (win10x64).

The problem is that, I have my passwords gathered all these years like this:
each username/password pair in a separate text file,
having as filename the domain name of the relevant password,
and inside each .txt file, the first line is the username, and the 2nd the password (both without a prefix).

For example:

Filename: reddit.com (it also has the .txt extension but that can be omitted) Contents:

my_username  
my_Password


In order the above to become an CSV it must be converted into:

"Account","Login Name","Password","Web Site", "Notes"
"Entry 1","my_username","my_password","http://reddit.com", ""

i.e.

"Account","Login Name",  "Password",    "Web Site",          "Notes"
"Entry 1","my_username", "my_password", "http://reddit.com", ""

Note: if there is a comma (,), double quote(") or backslash(\) in the password then it must be escaped with a backslash.

How can this be done via a bash script (cygwin)?


The code snippets I have gathered so far:

  1. How can I prepend a string to the beginning of each line in a file? (source)

    awk '{print "prefix" $0}' file
    
  2. Print nth Line From file.txt (source)

    awk "NR==2{print;exit}" file.txt                // for printing the 2nd line
    
  3. execute a command to all found files recursively

    find . -name "*.txt" -execdir *command*
    

    or alternatively

    for file in /cygdrive/c/folder/*; do 
         *command*                    // command example:   mv "$file" "${file}/.."   
    done
    


So, my script should start with:

find . -name "*.txt" -execdir        awk '{print '"Account","Account","Login Name","Password","Web Site", "Notes"' $0}' "$file"

I'm stuck at this point.

4

Input files:

  1. yahoo.net.txt
  2. gougle.com.txt
  3. reddit.com.txt

Contents of yahoo.net.txt:

user1-yaho0
pas,,

Contents of gougle.com.txt:

user1-google
pas"wor,d

Contents of reddit.com.txt:

user1-reddit
pas\wor\d

With this input, the following quick and dirty script does the job:

#!/bin/bash

echo "\"Account\",\"Login Name\",\"Password\",\"Web Site\",\"Notes\"" > output.csv
num=0
for f in `find . -type f -name '*.txt'` ; do

num=$((num + 1))
user=`head -1 "$f"`
password=`sed 1d "$f" | sed -r 's/([\\,"])/\\\\\1/g'`
domain=`basename "$f" | sed 's/\.txt$//'`

echo "\"$num\",\"$user\",\"$password\",\"$domain\",\"\"" >> output.csv

done

Contents of output.csv:

"Account","Login Name","Password","Web Site","Notes"
"1","user1-reddit","pas\\wor\\d","reddit.com",""
"2","user1-yaho","pas\,\,","yahoo.net",""
"3","user1-google","pas\"wor\,d","gougle.com",""
0

With awk

 awk 'BEGIN{print "\"Account\",\"Login Name\",\"Password\",\"Web Site\",\"Notes\""}
{ if (FNR==1){
         user=$0;a+=1};
  if(FNR==2){
         print "\""a"\",\""user"\",\""$0"\",\""gensub(/\.txt/,"","",FILENAME)"\","}}' *.txt

Use awk special variable FNR to get the 1st and 2nd line from each *.txt file and string manipulation function gensub to remove .txt from FILENAME( an awk special variable) for URL column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.