Convert tab separated text to non-trivial xml. (pwsafe --> KeePassx)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert tab separated text to non-trivial xml. (pwsafe --> KeePassx)
# 1  
Old 06-14-2015
Blade Convert tab separated text to non-trivial xml. (pwsafe --> KeePassx)

I'd like to take the output of `pwsafe --exportdb > database.txt` and convert it to a KeePassX XML friendly format (feature request in pwsafe).

I found flat file converter but the syntax is beyond me with this example. Solutions are welcomed.

More details
Here is the pwsafe --> KeePassX XML translations. The pwsafe export is simply a txt file with 6 fields (the first field can be ignored):
Code:
uuid= doesn't translate
group= group>title
name= entry>title
login= entry>username
passwd= entry>password
notes= entry>comment

Example txt file for conversion (exported from pwsafe):
Code:
# passwordsafe version 2.0 database"
uuid	group	name	login	passwd	notes
"123d9-daf-df-3423423"	"retail"	"amazon"	"myamazonuser"	"sjfJ849"	"superfluous comment"
"4599d934-dsfs-324"	"retail"	"netflix"	"netflixuser"	"dj3W$#"	""
"4kdfkd-434-jj"	"email"	"gmail"	"mygmail"	"dfkpass"	""

Example xml in keepassx xml:
Code:
<!DOCTYPE KEEPASSX_DATABASE>
<database>
 <group>
  <title>Internet</title>
  <entry>
   <title>github</title>
   <username>githubusername</username>
   <password>githubpassword</password>
   <comment>optional comment</comment>
  </entry>
 </group>
 <group>
  <title>retail</title>
  <entry>
   <title>amazon</title>
   <username>username</username>
   <password>myamazonpw</password>
  </entry>
 </group>
 <group>
  <title>retail</title>
  <entry>
   <title>netflix</title>
   <username>username</username>
   <password>mynfxpw</password>
  </entry>
 </group>
</database>

# 2  
Old 06-14-2015
Create a file named template with this content:

Code:
--HEADER--
<!DOCTYPE KEEPASSX_DATABASE>
<database>
--ROW--
  <group>
    <title>%%FIELD#02%%</title>
    <entry>
       <title>%%FIELD#03%%</title>
       <username>%%FIELD#04%%</username>
       <password>%%FIELD#05%%</password>
       <comment>%%FIELD#06%%</comment>
    </entry>
  </group>
--FOOTER--
</database>


Then you can use this code:

Code:
awk -F\" '
FNR==NR&&/^--/ {section++; next}
FNR==NR{block[section]=(block[section]?block[section]"\n":"") $0;next}
FNR==1{print block[1]}
$1 != "uuid" && ! /^#/ { out=block[2]
  for(i=1;i<=NF;i++)
     gsub(sprintf("%%%%FIELD#%02d%%%%",i),$(i*2),out);
  print out
}
END{print block[3]}' template database.txt


Edit: Questions

Could the data contain characters needing to be escaped in XML (namely double quotation mark, ampersand, apostrophe, less-than or greater-than signs)?
If the data did contains double quotation marks how would they appear in the datbase.txt file?

Last edited by Chubler_XL; 06-14-2015 at 10:12 PM.. Reason: Support spaces within quoted field values
# 3  
Old 06-15-2015
Thanks for the reply. It turns out that the problem was more complex than I indicated due to the need to sort and group like entries. To illustrate, take the example I provided above: the two "retail" groups should appear together in the generated xml.

Someone was kind enough to create a python script that does this replying to my post (way over my head): https://github.com/mauritiusdadd/pwsafe2keepassx
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert Excel File (xls) to tab delimited text file on AIX

Hi i have a problem in my job i try to convert an excel file (xls extention) to text file (tab delimited), but no result with this comand cat xxx.xls > xxx.txt Do you have eny idea? PS: sorry for my english Thanks!! (4 Replies)
Discussion started by: frisso
4 Replies

2. Shell Programming and Scripting

Gawk output separated by tab

In the gawk below, I am trying to output the file tab-deliminated but don't think that is the correct syntax. Thank you :). gawk OFS='/t' '{sub(/-+/,"",$2); ar=$0} END{n = asort(ar) for (i = 1; i <= n; i++) print ar}' file (2 Replies)
Discussion started by: cmccabe
2 Replies

3. UNIX for Dummies Questions & Answers

Need to convert a pipe delimited text file to tab delimited

Hi, I have a rquirement in unix as below . I have a text file with me seperated by | symbol and i need to generate a excel file through unix commands/script so that each value will go to each column. ex: Input Text file: 1|A|apple 2|B|bottle excel file to be generated as output as... (9 Replies)
Discussion started by: raja kakitapall
9 Replies

4. Shell Programming and Scripting

Problem with a tab separated file

Hi, I have created a tab separated file from the following input file. ADDRESS1 CITY STATE POSTAL COUNTRY LON LAT 32 PRINZREGENTENSTRASSE ROSENHEIM BAYERN 83022 DEU 1212182 4785699 263 VIA DANTE ALIGHIERI BARI PUGLIA 70122 ITA 1686233 4112154 30 VIA MILANO ... (1 Reply)
Discussion started by: ramky79
1 Replies

5. Shell Programming and Scripting

replace row separated by tab

Dear users, I have this problem, this is the example: 123 (tab) A (tab) B (tab) C (tab) 456 where the (tab) is actually the \t delimiter. I need to replace the A B and C for D E and F, this is: 123 (tab) D (tab) E (tab) F (tab) 456 The thing is that my file is quite long and this... (2 Replies)
Discussion started by: Gery
2 Replies

6. UNIX for Dummies Questions & Answers

How to convert a text file into tab delimited format?

I have a text file that made using text editor in Ubuntu. However the text file is not being recognized as space or tab delimited, the formatting seems to be messed up. How can I convert the text file into tab delimited format? (3 Replies)
Discussion started by: evelibertine
3 Replies

7. Shell Programming and Scripting

Convert a tab separated file using bash

Dear all, I have a file in this format (like a matrix) - A B C .. X A 1 4 2 .. 2 B 2 6 4 .. 8 C 3 5 5 .. 4 . . . ... . X . . ... . and want to convert it into a file with this format: A A = 1 A B = 4 A C = 2 ... A X = 2 B A = 2 B B = 6 etc (2 Replies)
Discussion started by: TheTransporter
2 Replies

8. UNIX for Dummies Questions & Answers

How to convert text to columns in tab delimited text file

Hello Gurus, I have a text file containing nearly 12,000 tab delimited characters with 4000 rows. If the file size is small, excel can convert the text into coloumns. However, the file that I have is very big. Can some body help me in solving this problem? The input file example, ... (6 Replies)
Discussion started by: Unilearn
6 Replies

9. Shell Programming and Scripting

Compare two columns separated by a tab

witam potrzebuje polecenia porownujacego koumny na podstawie n-ostatnich znakow danej linnijki tj mam 2 koumny AiB zawierajace ciag dowolnych znakow (dlugosci w kazdej linijce mga byc rozne wiec uzycie substra odpada) A B ewewewabc nbgujnnabc... (3 Replies)
Discussion started by: Toudi
3 Replies

10. UNIX for Dummies Questions & Answers

Sum up a decimal column in a tab separated text file and error handling

Hi, I have a small requirement where i need to sum up a column in a text file. Input file 66ab 000000 534385 -00000106350.00 66cd 000000 534485 -00013364511.00 66ad 000000 534485 -00000426548.00 672a 000000 534485 000000650339.82... (5 Replies)
Discussion started by: pssandeep
5 Replies
Login or Register to Ask a Question