Sponsored Content
Top Forums Shell Programming and Scripting Convert tab separated text to non-trivial xml. (pwsafe --> KeePassx) Post 302947018 by Chubler_XL on Sunday 14th of June 2015 05:16:55 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
FORMS(3)						   BSD Library Functions Manual 						  FORMS(3)

NAME
field_back, field_fore, field_pad, set_field_back, set_field_fore, set_field_pad -- form library LIBRARY
Curses Form Library (libform, -lform) SYNOPSIS
#include <form.h> chtype field_back(FIELD *field); chtype field_fore(FIELD *field); int field_pad(FIELD *field); int set_field_back(FIELD *field, chtype attribute); int set_field_fore(FIELD *field, chtype attribute); int set_field_pad(FIELD *field, int pad); DESCRIPTION
Calling the function field_back() will return the character attributes that will be applied to a field that is not the current field, these attributes can be set by the set_field_back() function. The field_fore() function returns the character attributes that will be used to indicate that a field is the currently active one on the form, this attribute may be set by using the set_field_fore() function. The pad character for a field is the character that will be printed in all field locations not occupied with actual field contents. The pad charac- ter can be retrieved by calling the field_pad() function, the pad character is set by using the set_field_pad() function. RETURN VALUES
Functions returning pointers will return NULL if an error is detected. The functions that return an int will return one of the following error values: E_OK The function was successful. SEE ALSO
curses(3), forms(3) NOTES
The header <form.h> automatically includes both <curses.h> and <eti.h>. BSD
January 1, 2001 BSD
All times are GMT -4. The time now is 09:53 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy