1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
|
\input texinfo @c -*-texinfo-*-
@c This file uses the @command command introduced in Texinfo 4.0.
@c %**start of header
@setfilename ffe.info
@settitle ffe - flat file extractor
@finalout
@c %**end of header
@include version.texi
@ifinfo
@dircategory Utilities
@direntry
* ffe: (ffe). Flat File Extractor.
@end direntry
@end ifinfo
@copying
This file documents version @value{VERSION} of @command{ffe}, a flat file extractor.
Copyright @copyright{} 2014 Timo Savinen
@quotation
Permission is granted to make and distribute verbatim copies of
this manual provided the copyright notice and this permission notice
are preserved on all copies.
Permission is granted to copy and distribute modified versions of this
manual under the conditions for verbatim copying, provided that the entire
resulting derived work is distributed under the terms of a permission
notice identical to this one.
Permission is granted to copy and distribute translations of this manual
into another language, under the above conditions for modified versions.
@end quotation
@end copying
@titlepage
@title ffe
@subtitle flat file extractor
@subtitle Version @value{VERSION}, @value{UPDATED}
@author by Timo Savinen
@page
@vskip 0pt plus 1filll
@insertcopying
@end titlepage
@c All the nodes can be updated using the EMACS command
@c texinfo-every-node-update, which is normally bound to C-c C-u C-e.
@ifnottex
@node Top, Overview, (dir), (dir)
@top ffe
@insertcopying
@end ifnottex
@c All the menus can be updated with the EMACS command
@c texinfo-all-menus-update, which is normally bound to C-c C-u C-a.
@menu
* Overview:: Preliminary information.
* Samples:: Samples using @command{ffe}.
* Invoking ffe:: How to run @command{ffe}.
* ffe configuration:: How @command{ffe} works.
* Problems:: Reporting bugs.
@end menu
@node Overview, Samples, Top, Top
@chapter Preliminary information
@cindex greetings
@cindex overview
The @command{ffe} is a program to extract fields from text and binary flat files and to print them in different
formats. The input file structure and printing definitions are specified in a configuration file, which
is always required. Default configuration file is @file{~/.fferc} (@file{ffe.rc} in windows).
@command{ffe} is a command line tool developed for GNU/Linux and UNIX systems. @command{ffe} can read from
standard input and write to standard output, so it can be used as a part of a pipeline.
There is also binary distribution for windows.
@node Samples, Invoking ffe, Overview, Top
@chapter Samples using @command{ffe}
@cindex sample
One example of using @command{ffe} for printing personnel information in XML format from fixed length flat file:
@example
$ cat personnel
john Ripper 23
Scott Tiger 45
Mary Moore 41
$
@end example
@noindent
A file @file{personnel} contains three fixed length fields: @samp{FirstName}, @samp{LastName} and @samp{Age},
their respective lengths are 9,13 and 2.
@noindent
In order to print data above in XML, following configuration file must be available:
@example
$cat personnel.fferc
structure personel @{
type fixed
output xml
record person @{
field FirstName 9
field LastName 13
field Age 2
@}
@}
output xml @{
file_header "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
data "<%n>%t</%n>\n"
record_header "<%r>\n"
record_trailer "</%r>\n"
indent " "
@}
$
@end example
@noindent
Using ffe:
@example
$ffe -c personnel.fferc personnel
<?xml version="1.0" encoding="ISO-8859-1"?>
<person>
<FirstName>john</FirstName>
<LastName>Ripper</LastName>
<Age>23</Age>
</person>
<person>
<FirstName>Scott</FirstName>
<LastName>Tiger</LastName>
<Age>45</Age>
</person>
<person>
<FirstName>Mary</FirstName>
<LastName>Moore</LastName>
<Age>41</Age>
</person>
$
@end example
@node Invoking ffe, ffe configuration, Samples, Top
@chapter How to run @command{ffe}
@cindex running ffe
@cindex using
@command{ffe} is a command line tool. Normally @command{ffe} can be invoked as:
@code{ffe -o OUTPUTFILE INPUTFILE@dots{}}
@noindent
@command{ffe} uses the definitions from the configuration file and tries to guess the input file
structure.
@noindent
If the structure cannot be guessed the option @option{-s} must be used.
@menu
* Invocation:: Program invocation
* Configuration:: Input and printing definitions
* Guessing:: How ffe identifies input structure
* Limits:: Limitations
@end menu
@node Invocation, Configuration, , Invoking ffe
@section Program invocation
@cindex options
@noindent
The format for running the @command{ffe} program is:
@example
ffe @var{option} @dots{}
@end example
@noindent
@command{ffe} supports the following options:
@c Formatting copied from the Texinfo 4.0 manual.
@table @code
@item -c @var{file}
@itemx --configuration=@var{file}
Configuration is read from @var{file}, instead of @file{~/.fferc} (@file{ffe.rc} in windows).
@item -s @var{structure}
@itemx --structure=@var{structure}
Use structure @var{structure} for input file, suppresses guessing.
@item -p @var{output}
@itemx --print=@var{output}
Use output format @var{output} for printing. If not given, then the record or structure related
output format is used. Printing can be suppressed using format @var{no}. Original data is printed using format @var{raw}.
@item -o @var{file}
@itemx --output=@var{file}
Write output to @var{file} instead of standard output.
@item -f @var{list}
@itemx --field-list=@var{list}
Print only fields and constants listed in the comma separated list @var{list}. Order of names in
@var{list} specifies also the printing order.
@item -e @var{expression}
@itemx --expression=@var{expression}
Print only those records for which the @var{expression} evaluates to true.
@item -a
@itemx --and
Expressions are combined with logical and, default is logical or.
Note that if the same field and operator appear several time in expressions they are always compared with logical or.
@item -X
@itemx --casecmp
Expressions are evaluated using case insensitive comparison
@item -v
@itemx --invert-match
Print only those records which don't match the expression.
@item -l
@itemx --loose
Normally @command{ffe} stops when it encounters an input line or binary block which doesn't match any of
the records in selected structure. Defining this option causes @command{ffe} continue despite the error.
Note that invalid lines are reported only for text input. In case of binary input next valid block is silently searched.
@item -r
@itemx --replace=@var{field}=@var{value}
Replace @var{field}s contents with @var{value} in output. @var{value} can contain same directives as output option @code{data}.
@item -d
@itemx --debug
All invalid input lines are written to @file{ffe_error_<pid>.log}, where @file{<pid>} is the process ID.
@item -I
@itemx --info
Show structure information in the configuration file and exit successfully. For every structure following information in shown:
@*
Structures: Name, type and maximum record length.
@*
Records: Name and length
@*
Fields: Name, position and length. First position is number one.
@item -?
@itemx --help
Print an informative help message describing the options and then exit
successfully.
@item -V
@itemx --version
Print the version number of @command{ffe} and then exit successfully.
@end table
All remaining options are names of input files, if no input files are specified or @code{-} is given, then the standard input is read.
@subheading Expressions (option @option{-e}, @option{--expression})
Expression can be used to select specific records comparing field values.
Expression has syntax @var{field}@strong{x}@var{value}, where @strong{x} is the comparison operator.
Expression is used to compare field's contents to @var{value} and if comparison is successful
the record is printed. Several expressions can be given and at least one must evaluate to true in
order to print a record. If option @option{-a} is given all expressions must evaluate to true.
If @var{value} starts with string @code{file:} then the rest of @var{value} is considered as a file name.
Every line in file is used as @var{value} in comparison. Comparison evaluates true if one or more values matches, so this makes possible use several different values in comparison. @strong{Note}: The file size is limited by available memory because the file contents is loaded to memory.
When comparing binary fields the @var{value} must have the representation which can be shown using the @code{%d} output directive. Note that the printing option @var{hex-caps} takes effect in comparison.
@noindent
Expression notation:
@table @var
@item field@strong{=}value
Field @var{field} is equal to @var{value}.
@item field@strong{^}value
Field @var{field} starts with @var{value}.
@item field@strong{~}value
Field @var{field} contains @var{value}.
@item field@strong{!}value
Field @var{field} is not equal to @var{value}.
@item field@strong{?}value
Field @var{field} matches the regular expression @var{value}.
@command{ffe} supports POSIX extended regular expressions.
@end table
@node Configuration, Guessing, Invocation, Invoking ffe
@section Configuration
@cindex configuration
@command{ffe} uses configuration file in order to read the input file and print the output.
Configuration file for @command{ffe} is a text file. The file may contain empty lines.
Commands are case sensitive. Comments begin with the @code{#}-character and end at the end of the line.
The @code{string} definitions can be enclosed in double quotation @code{"} characters.
@code{char} is a single character. @code{string} and @code{char} can contain following escape codes:
@code{\a}, @code{\b}, @code{\t}, @code{\n}, @code{\v}, @code{\f}, @code{\r}, @code{\"} and @code{\#}.
A backslash can be escaped as @code{\\}.
Configuration has two main parts: the structure, which specifies the input file structure and
the output, which specifies how the input data is formatted for output.
@subheading Common syntax
Common syntax for configuration file is:
@example
#comment
`command`
const @var{name} @var{value}
filter @var{name} @var{value}
@dots{}
structure @var{name} @{
@i{option value} @dots{}
@dots{}
record @var{name} @{
@i{option value} @dots{}
@dots{}
@}
record @var{name} @{
@i{option value} @dots{}
@dots{}
@}
@dots{}
@}
structure @var{name} @{
@dots{}
@}
@dots{}
output @var{name} @{
@i{option value} @dots{}
@dots{}
@}
output @var{name} @{
@dots{}
@}
@dots{}
lookup @var{name} @{
@i{option value} @dots{}
@dots{}
@}
lookup @var{name} @{
@dots{}
@}
@dots{}
@end example
@subheading Structure
Keyword @code{structure} is used to specify the input file content. An input file can contain several
types of records (lines or binary blocks). E.g. file can have a header, data and trailer record types. Records
must be distinguishable from each other, this can be achieved defining different 'keys'
(@code{id} in record definition) or having different line lengths (for fixed length) or different count
of fields (for separated structure) for different records.
If binary structure has several records, then all records must have at least one key (@code{id}), because binary blocks can
be distinguished only by using keys.
@noindent
The structure notation:
@*
@example
structure @var{name} @{
@i{option value} @dots{}
@dots{}
@}
@end example
@noindent
A structure can contain following options:
@table @code
@item type fixed|binary|separated [@var{char}] [*]
The fields in the input are fixed length fields (text or binary) or text fields separated by @var{char}. If * is given,
multiple sequential separators are considered as one. Default separator is comma.
@item quoted [@var{char}]
Fields may be quoted with char, default quotation mark is the double quotation mark '"'.
A quotation mark is assumed to be escaped as \@var{char} or doubling the mark as @var{charchar} in input.
Non escaped quotation marks are not preserved in output.
@item header first|all|no
Controls the occurrence of the header line. Default is no. If set as @emph{first} or @emph{all}, the first line
of the first input file is considered as header line containing the names of the fields. @emph{first}
means that only the first file has a header, @emph{all} means means that all files have a header,
although the names are still taken from the header of the first file. Header line is handled
according the record definition, meaning that the name positions, separators etc. are the same as
for the fields. Binary files cannot have a header.
@item output @var{name}|no|raw
All records belonging to this structure are printed according output format name.
Default is to use output named as @samp{default}. @samp{no} prints nothing and @samp{raw} prints only the original data.
@item record @var{name} @{@i{options} @dots{}@}
Specifies one record for a structure. A structure can contain several record types.
@end table
@subheading Record
A record specifies one type of input line or binary block in a file. Different records can be distinguished using
the @code{id} option or different line lengths or field counts. In multi-record binary structure every record must have at least one @code{id} because binary records do not have a special end of record marker as text lines have.
@noindent
The record notation:
@*
@example
record @var{name} @{
@i{option value} @dots{}
@dots{}
@}
@end example
@noindent
A record can contain following options:
@table @code
@item id @var{position} @var{string}
@itemx rid @var{position} @var{regexp}
Identifies a record in the input file. Records are identified by the @var{string} or by the regular expression @var{regexp} in input record position
@var{position}. For fixed length and binary input the position is the byte position of input record and for
separated input the @var{position} is the @var{position}'th field of the input record. Positions starts always from one.
A record definition can contain several id's, then all id's must match the input line
(@code{id}'s are @emph{and-ed}).
Non printable characters can be escaped as @samp{\xnn}, where @samp{nn} is characters hexadecimal value.
@item field @var{name}|FILLER|* [@var{length}]|* [@var{lookup}]|* [@var{output}]|* [@var{filter}]|* [@var{conversion}]
Defines a field in a text input structure. @var{length} is mandatory for fixed length input structure.
The last field of a fixed length input structure can have a @emph{*} in place of @var{length}. That means that the last field
has no exact length specified and it gets the remainder of the input line after all other fields. This allows a
fixed record to have arbitrary long last field.
Length is also used for printing the fields in fixed length format (directive @code{%D} in output definitions).
If @emph{*} is given instead of the name, then the @var{name} will be the ordinal number of the field,
or if the @code{header} option has value @emph{first} or @emph{all}, then the name of the field will be taken from
the header line (first line of the input).
If @var{lookup} is given then the fields contents is used to make a lookup in lookup table @var{lookup}.
If @var{length} is not needed (separated format) but lookup is needed, use asterisk (*) in place of length definition.
If @var{output} is given the field will be printed using output definition @var{output}. If @var{length} and/or @var{lookup} are not needed use asterisk in place of them. Use asterisk (*) if not needed.
If @var{filter} is given the raw contents of the field is filtered through a program defined by @var{filter} and the output of the program is printed as field contents.
If @var{conversion} is given it should contain a single printf style conversion specification, which will be used in printing. Conversion specification must start with @code{%} and the last character must be from set @code{diuoxXfeEgGcs}.
If field is named as @code{FILLER}, the field will not appear in output.
The order of fields in configuration file is essential, it specifies the field order in a record.
@item field @var{name}|FILLER|* @var{length}|@var{type} [@var{lookup}]|* [@var{output}]|* [@var{filter}]|* [@var{conversion}]
Defines a field in a binary structure. All other features are same as for text structure fields except the @var{type} parameter.
@var{type} specifies the field length and type and can have the following values:
@table @code
@item char
Printable character.
@item short
Short integer having current system length and byte order.
@item int
Integer having current system length and byte order.
@item long
Long integer having current system length and byte order.
@item llong
Long long integer having current system length and byte order.
@item ushort
Unsigned short integer having current system length and byte order.
@item uint
Unsigned integer having current system length and byte order.
@item ulong
Unsigned long integer having current system length and byte order.
@item ullong
Unsigned long long integer having current system length and byte order.
@item int8
8 bit integer.
@item int16_be
Big endian 16 bit integer.
@item int32_be
Big endian 32 bit integer.
@item int64_be
Big endian 64 bit integer.
@item int16_le
Little endian 16 bit integer.
@item int32_le
Little endian 32 bit integer.
@item int64_le
Little endian 64 bit integer.
@item uint8
Unsigned 8 bit integer.
@item uint16_be
Unsigned big endian 16 bit integer.
@item uint32_be
Unsigned big endian 32 bit integer.
@item uint64_be
Unsigned big endian 64 bit integer.
@item uint16_le
Unsigned little endian 16 bit integer.
@item uint32_le
Unsigned little endian 32 bit integer.
@item uint64_le
Unsigned little endian 64 bit integer.
@item float
Float having current system length and byte order.
@item float_be
Float having current system length and big endian byte order.
@item float_le
Float having current system length and little endian byte order.
@item double
Double having current system length and byte order.
@item double_be
Double having current system length and big endian byte order.
@item double_le
Double having current system length and little endian byte order.
@item bcd_be_@var{len}
Bcd number having length @var{len} and nybbles in big endian order.
@item bcd_le_@var{len}
Bcd number having length @var{len} and nybbles in little endian order.
@item hex_be_@var{len}
Hexadecimal data in big endian order having length @var{len}.
@item hex_le_@var{len}
Hexadecimal data in little endian order having length @var{len}.
@end table
If @var{length} is given instead of the @var{type}, then the field is assumed to be a printable string having length @var{length}. String is printed until @var{length} characters are printed or NULL character is found.
Bcd number (@code{bcd_be_@var{len}} and @code{bcd_le_@var{len}}) is printed until @var{len} bytes are read or a nybble having hexadecimal value @code{f} is found.
Bcd number having big endian order is printed in order: most significant nybble first and least significant nybble second and bcd number having little endian order is printed in order: least significant nybble first and most significant nybble second. Bytes are always read in big endian order.
Hexadecimal data (@code{hex_be_@var{len}} and @code{hex_le_@var{len}}) is printed as hexadecimal values. Big endian data is printed starting from lower address and little endian data starting from upper address.
@item field-count @var{number}
Same effect as having "@code{field *}" @var{number} times. This can be used in separated structure instead of
writing sequential "@code{field *}" definitions. Several @code{field-count}s can be used in the same record and
they can be mixed with @code{field}.
@item fields-from @var{record}
Fields in this record are the same as in record @var{record}. @code{field} and @code{fields-from} are mutually
exclusive.
@item output @var{name}|no|raw
This record is printed according to output format @var{name}. Default is to use output format specified in structure.
@item level @var{number} [@var{element_name}|*] [@var{group_name}]
Levels can be used to print the file in hierarchical multi-level nested form document.
@var{number} is the level of the record, starting from number one (highest level),
@var{element_name} is the name for the record, @var{group_name}
is used to group records in the same and lower levels. Only @var{number} is mandatory.
Use * instead of the element name if group name is needed.
@item record-length strict|minimum
@table @code
@item strict
Input record length (fixed format) or field count (separated format) must match the record definition in order to get it processed. This is the default value.
@item minimum
Input record length or field count can be the same or longer as defined for the record. The rest of the input line is ignored.
@end table
@item variable-length @var{record_length} @var{variable_length_field} @var{adjust}
@var{record_length} and @var{variable_length_field} are the names of two fields in the record and @var{adjust} is a signed integer.
Record length is read from field @var{record_length}. @var{record_length} is assumed to be an integer type for binary structures or contain only decimal numbers in fixed length structure.
@var{record_length} is assumed to contain the total length of the record.
@var{variable_length_field} is the field having variable length. The length of @var{variable_length_field} is calculated by subtracting the total length of the all other fields from the length read from @var{record_length}.
@*
The length given by keyword @var{field} for @var{variable_length_field} is ignored. After calculating the length it is adjusted by @var{adjust}. @var{adjust} can be used in cases where the length read from @var{variable_length_field} does not contain the total length of the record. variable-length can be used with binary or fixed lengths structures only.
@end table
@subheading Output
Keyword @code{output} specifies a output format for formatting the input data for output. Formatting
is controlled using options and printf style directives. An output definition is independent
from structure, so one output format can be used with different input file formats.
@noindent
The output notation:
@*
@example
output @var{name} @{
@i{option value} @dots{}
@dots{}
@}
@end example
@noindent
Actual formatting and printing is controlled using @emph{pictures} in output options. Pictures can contain
following printf style directives:
@table @code
@item %f
Name of the input file.
@item %s
Name of the current structure.
@item %r
Name of the current record.
@item %o
Input record number in current file.
@item %O
Input record number starting from the first file.
@item %i
Byte offset of the current record in the current file. Starts from zero.
@item %I
Byte offset of the current record starting from the first file. Starts from zero.
@item %n
Field name.
@item %t
Field contents, without leading and trailing white-spaces.
@item %d
Field contents. Binary integer is printed as a decimal value. Floating point number is printed in the style @code{[-]ddd.ddd}, where the number of digits after the decimal-point character is 6. Bcd number is printed as a decimal number and hexadecimal data as consecutive hexadecimal values.
@item %D
Field contents, right padded to the field length (requires length definition for the field).
@item %C
Field contents, right padded to the field length (requires length definition for the field). Contents is cut if the input field
is longer than output length.
@item %x
Unsigned hexadecimal value of a binary integer. Other fields are printed as directive @code{%d} would be used.
@item %l
Lookup value which has been found using current field as a search key.
@item %L
Lookup value, right padded to the field length.
@item %p
Fields start position in a record. For fixed and binary structure this is field's byte position in the input line
and for separated structure this is the ordinal number of the field. Starts from one.
@item %h
Hexadecimal dump of a field. Byte values are printed as consecutive @code{xnn} values, where the @code{nn} is the hexadecimal value of a byte. Data is printed before any endian conversion.
@item %e
Does not print anything, causes still the "field empty" check to be performed.
Can be used when only the names of non-empty fields should be printed.
@item %g
Group name given by the keyword @code{group_name} in record definition.
@item %m
Element name given by the keyword @code{element_name} in record definition.
@item %%
Percent sign.
@end table
@noindent
Output options:
@table @code
@item file_header @var{picture}
@var{picture} is printed once before file contents.
@item file_trailer @var{picture}
@var{picture} is printed once after file contents.
@item header @var{picture}
If given, then the header line describing the field names is printed before records.
Every field name is printed according the @var{picture} using the same separator and field length as
given for the fields. Picture can contain only @code{%n} directive.
@item data @var{picture}
Field contents is printed according @var{picture}.
@item lookup @var{picture}
If current field is related to lookup table, then this @var{picture} is used instead of picture from @code{data}.
This makes possible to use different picture when the field is related to a lookup table. Default is to use the picture from @code{data}.
@item separator @var{string}
All fields are terminated by @var{string}, except the last field of the record.
Default is not to print separator.
@item record_header @var{picture}
@var{picture} is printed before the record content. Default is not to print the record header.
@item record_trailer @var{picture}
@var{picture} is printed after the record content. Default is newline.
@item justify left|right|@var{char}
The output from the @code{data} option is left or right justified.
@var{char} justifies output according the first occurrence of @var{char}
in the data picture. Default is left.
@item indent @var{string}
Record contents is intended by @var{string}.
Field contents is intended by two times the string. Default is not to indent.
If file contents is printed in hierarchical form (keyword @code{level} in record definition) then
contents is indented according the level of a record.
@item field-list @var{name1},@var{name2},@dots{}
Only fields and constants named as @var{name1},@var{name2},@dots{} are printed, same effect as has option @option{-f}.
Default is print all fields and no constants. Fields and constants are also printed in the same order as they are listed.
@item no-data-print yes|no
If @code{field-list} is given and and this is set as no and none of the fields in @code{field-list}
does not belong to the current record, then the @code{record_header} and @code{record_trailer} are not printed.
Default is yes.
@item field-empty-print yes|no
When set as no, nothing is printed for the fields which consist entirely of characters from @code{empty-chars}.
If none of the fields of a record are printed, then the printing of @code{record_trailer} is also suppressed.
Default is yes.
@item empty-chars @var{string}
@var{string} specifies a set of characters which consist an "empty" field. Default is
@w{" \f\n\r\t\v"} (space, form-feed, newline, carriage return, horizontal tab and vertical tab).
@item output-file @var{file}
Output is written to @var{file} instead of the default output (standard output or given by @option{-o, --output}).
If - is given the output is written to standard output.
@item group_header @var{picture}
If a record has a level and a group name defined,
@var{picture} is printed before the first record in a group or if the group name has changed in the same level.
@strong{Note}: Level related pictures can contain printing directives @code{%g} and @code{%n} only.
@item group_trailer @var{picture}
If a record has a level and a group name defined,
@var{picture} is printed after the records in lower levels are printed or if the group name has changed in the
same level or if a higher level record is found.
@item element_header @var{picture}
If a record has a level and a element name defined, @var{picture} is printed before the records contents.
@item element_trailer @var{picture}
If a record has a level and a element name defined, @var{picture} is printed after the records contents or after
the following lower level records.
@item hex-caps yes|no
Print hexadecimal numbers in capital letters. Default is no.
@end table
@subheading Lookup
Keyword @code{lookup} specifies a lookup table which can be searched using field contents. Found values can
be printed using output directives @code{%l} and @code{%L}.
@noindent
The lookup table notation:
@*
@example
lookup @var{name} @{
@i{option value} @dots{}
@dots{}
@}
@end example
@noindent
Lookup options:
@table @code
@item search exact | longest
Search method for this table. Either exact or longest match is used when searching the table. Default is @code{exact}.
@item pair @var{key} @var{value}
Defines a key/value pair for the lookup table. In case of binary file @var{key} must have the same representation as
can be shown using the @code{%d} printing directive.
@item file @var{name} [@var{separator}]
Data for the lookup table is read from file @var{name}. Each line in file @var{name} is considered as a key/value pair
separated by a single character @var{separator}. Default separator is semicolon. Lines without separator are silently omitted.
@strong{Note}: The file size is limited by available memory because the file contents is loaded to memory.
@item default-value @var{value}
If searching the lookup table is unsuccessful then @var{value} is used in printing. Default is empty string.
@end table
@subheading Constants
Keyword @code{const} specifies one name/value pair which can be used as an additional output field.
Constants can be used only in field lists (option @option{-f,--field-list}, or output option @code{field-list}).
Constants can be used to add fields to output which do not appear in input. E.g. new fields for
separated output or adding spaces after a fixed length field (changing the field length).
Note that @var{value} is printed as it is for every record. It cannot be changed record by record.
If a constant has the same name as one of the input fields, the value @var{value} is printed instead of
the input field contents.
The constant notation:
@*
@example
const @var{name} @var{value}
@end example
When @var{name} appears in field list it is treated as one of the input fields having contents @var{value}.
@subheading Filter
Keyword @code{filter} defines a command that can be used to format field raw contents. Command must read the standard input
and write to standard output and it must not block. Field raw contents is filtered through the command and the output is printed as
field contents.
The filter notation:
@*
@example
filter @var{name} @var{command}
@end example
@var{name} is referred in field definition. @var{command} is the shell command to be executed.
@subheading Anonymization
Keyword @code{anonymize} defines a set of fields which will be anonymized by using command line option @option{-A,--anonymize}
is given. Ffe uses non-reversible anonymization methods and preserves the original field length.
Notation:
@*
@example
anonymize @var{name} @{
@i{method} @dots{}
@dots{}
@}
@end example
The anonymization will be done if command line option @option{-A,--anonymize} is given with @var{name}.
@noindent
Anonymize options:
@table @code
@item method @var{field} @var{method} @var{start} @var{length} @var{parameter}
All fields named as @var{field} in the current structure will be anonymized using method @var{method}.
As default the whole field is anonymized. Some parts of the field can be left non-anonymized using
@var{start} and @var{length}. @var{start} is the byte position where the anonymization starts, first byte is number 1.
If @var{start} is negative the anonymization starts from the end of the field.
If @var{length} is given then @var{length} number of bytes is anonymized after start position, default value 0 means the rest of the field.
Only @var{field} and @var{method} are mandatory.
@*
@*
Values for @var{method}:
@table @code
@item MASK
Field will be masked with character '0'. Different character can be given with @var{parameter}.
@item RANDOM
@itemx NRANDOM
Field will be filled with randomly selected bytes.
@item HASH
@itemx NHASH
Field will be filled with data from hash calculated from the original field.
This method yields always the same result with same input. The hash length in bytes can be given with @var{parameter}.
Default hash length is 16, valid values for hash length are 16, 32 and 64.
@end table
Methods RANDOM and HASH use characters @code{0-9,A-Z,a-z} and space for text fields. Methods NRANDOM and NHASH use only characters @code{0-9}.
For binary fields all byte values are used. BCD coded fields are always filled with BCD values @code{0-9}.
@end table
@subheading Command Substitution
Command Substitution allows the output of a command to replace parts of the configuration file. Syntax for
command substitution is:
@*
@*
`@code{command}`
@*
@*
The @code{command} is executed and the `@code{command}` is substituted with the standard output of
the command, with any trailing newlines deleted. Command substitutions may not be nested.
Before executing the @code{command} @command{ffe} sets following environment variables:
@table @code
@item FFE_STRUCTURE
The name of the structure from @option{-s,--structure}.
@item FFE_OUTPUT
The name of the output file from @option{-o,--output}.
@item FFE_FORMAT
The name of the output format from @option{-p,--print}.
@item FFE_FIRST_FILE
The name of the first input file.
@item FFE_FILES
A space-separated list of all input files.
@end table
If variable is already set it will not be replaced.
@subheading Input Preprocessor
It is possible to define an input preprosessor for @command{ffe}. An input preprocessor is simply an executable program
which writes the contents of the input file to standard output which will be read by @command{ffe}. If the input preprosessor
does not write any characters on its standard output, then @command{ffe} uses the original file.
To set up an input preprocessor, set the @code{FFEOPEN} environment variable to a command line which will invoke your input preprocessor.
This command line should include one occurrence of the string @code{%s},
which will be replaced by the input filename when the input preprocessor command is invoked.
The input preprocessor is not used if @command{ffe} is reading standard input.
Convenient way is to use @command{lesspipe} (or @command{lesspipe.sh}), which is available in many UNIX-systems, for example
@*
@example
export FFEOPEN="/usr/bin/lesspipe %s"
@end example
Using the example above is it possible to give a zipped input file to @command{ffe}, then the input processor will unzip the
file before it is processed by @command{ffe}.
@node Guessing,Limits,Configuration,Invoking ffe
@section Guessing
@cindex guess
If @option{-s} is not given, @command{ffe} tries to guess the input structure.
When guessing binary data @command{ffe} reads the first block of input data and tries to match the structure definitions
from configuration file to that block. The input block size is the maximum binary block size found in configuration file.
When guessing text data @command{ffe} reads the first 10 000 lines or 1 MB of input data and tries to match the structure definitions
from configuration file to input stream. If all lines match one and only one structure, the structure is used
for reading the input file.
@noindent
Guessing uses following execution cycle:
@enumerate
@item
A input line or a binary block is read
@item
All record @code{id}'s are compared to the input data, if all @code{id}'s of a record match
the input date and the
records line length matches the total length (or total count for separated structure) of the fields,
the record is considered to match the input line. If there are no @code{id}'s,
only the line length or field count is checked. In case of binary data only @code{id}'s are used in matching.
@item
In case of text data: If all lines match at least one of the records in a particular structure, the structure is considered as selected.
There must be only one structure matching all lines used for guessing.
In case of binary data: If the first block matches at least one record of a structure, the structure is considered as selected. Only one structure must match.
@end enumerate
@node Limits, , Guessing, Invoking ffe
@section Limitations
@cindex big files
@cindex limits
At least in GNU/Linux @command{ffe} should be able to handle big files (> 4 GB), other
systems are not tested.
Regular expression can be used in operator @strong{?} in option @option{-e}, @option{--expression} and in record key word @code{rid} only in systems where
regular expression functions (regcomp, regexec, @dots{}) are available.
@node ffe configuration, Problems, Invoking ffe, Top
@chapter How @command{ffe} works
Following examples use two different input files:
@subheading Fixed length example
Fixed length personnel file with header and trailer, line (record) is identified by the
first byte (H = Header, E = Employee, B = Boss, T = trailer).
@example
$cat personnel.fix
H2006-02-25
EJohn Ripper 23
BScott Tiger 45
EMary Moore 41
ERidge Forrester 31
T0004
$
@end example
@noindent
Structure for reading file above. Note that record @samp{boss} reuses fields from @samp{employee}. Age will be printed three numbers long with padded zeros.
@example
structure personel_fix @{
type fixed
record header @{
id 1 H
field type 1
field date 10
@}
record employee @{
id 1 E
field EmpType 1
field FirstName 9
field LastName 13
field Age 2 * * * "%03d"
@}
record boss @{
id 1 B
fields-from employee
@}
record trailer @{
id 1 T
field type 1
field count 4
@}
@}
@end example
@subheading Separated example
Same file as above, but now separated by comma.
@example
$cat personnel.sep
H,2006-02-25
E,john,Ripper,23
B,Scott,Tiger,45
E,Mary,Moore,41
E,Ridge,Forrester,31
T,0004
$
@end example
@noindent
Structure for reading file above. Note that the field lengths are not needed in separated format. Length
is need if the separated data is to be printed in fixed length format.
@example
structure personel_sep @{
type separated ,
record header @{
id 1 H
field type
field date
@}
record employee @{
id 1 E
field type
field FirstName
field LastName
field Age
@}
record boss @{
id 1 B
fields-from employee
@}
record trailer @{
id 1 T
field type
field count
@}
@}
@end example
@subheading Printing in XML format
Data in examples above can be printed in XML using output definition like:
@example
output xml @{
file_header "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
data "<%n>%t</%n>\n"
record_header "<%r>\n"
record_trailer "</%r>\n"
indent " "
@}
@end example
@noindent
Example output using command (assuming definitions above are saved in ~/.fferc)
@code{ffe -p xml personnel.sep}
@example
<?xml version="1.0" encoding="UTF-8"?>
<header>
<type>H</type>
<date>2006-02-25</date>
</header>
<employee>
<type>E</type>
<FirstName>john</FirstName>
<LastName>Ripper</LastName>
<Age>23</Age>
</employee>
<boss>
<type>B</type>
<FirstName>Scott</FirstName>
<LastName>Tiger</LastName>
<Age>45</Age>
</boss>
<employee>
<type>E</type>
<FirstName>Mary</FirstName>
<LastName>Moore</LastName>
<Age>41</Age>
</employee>
<employee>
<type>E</type>
<FirstName>Ridge</FirstName>
<LastName>Forrester</LastName>
<Age>31</Age>
</employee>
<trailer>
<type>T</type>
<count>0004</count>
</trailer>
@end example
@subheading Printing sql commands
Data in examples above can be loaded to database by generated sql commands. Note that the header and trailer
are not loaded, because only fields @samp{FirstName},@samp{LastName} and @samp{Age} are printed and @samp{no-data-print}
is set as no. This prevents the @samp{record_header} and @samp{record_trailer} to be printed for file header and trailer.
@example
output sql @{
file_header "delete table boss;\ndelete table employee;\n"
record_header "insert into %r values("
data "'%t'"
separator ","
record_trailer ");\n"
file_trailer "commit\nquit\n"
no-data-print no
field-list FirstName,LastName,Age
@}
@end example
@noindent
Output from command
@code{ffe -p sql personnel.sep}
@example
delete table boss;
delete table employee;
insert into employee values('john','Ripper','23');
insert into boss values('Scott','Tiger','45');
insert into employee values('Mary','Moore','41');
insert into employee values('Ridge','Forrester','31');
commit
quit
@end example
@subheading Human readable output
This output format shows the fields in format suitable for displaying in screen or printing.
@example
output nice @{
record_header "%s - %r - %f - %o\n"
data "%n=%t\n"
justify =
indent " "
@}
@end example
@noindent
Output from command
@code{ffe -p nice personnel.fix}
@example
personel - header - personnel.fix - 1
type=H
date=2006-02-25
personel - employee - personnel.fix - 2
EmpType=E
FirstName=John
LastName=Ripper
Age=023
personel - boss - personnel.fix - 3
EmpType=B
FirstName=Scott
LastName=Tiger
Age=045
personel - employee - personnel.fix - 4
EmpType=E
FirstName=Mary
LastName=Moore
Age=041
personel - employee - personnel.fix - 5
EmpType=E
FirstName=Ridge
LastName=Forrester
Age=031
personel - trailer - personnel.fix - 6
type=T
count=0004
@end example
@subheading HTML table
Personnel data can be displayed as HTML table using output like:
@example
output html @{
file_header "<html>\n<head>\n</head>\n<body>\n<table border=\"1\">\n<tr>\n"
header "<th>%n</th>\n"
record_header "<tr>\n"
data "<td>%t</td>\n"
file_trailer "</table>\n</body>\n</html>\n"
no-data-print no
@}
@end example
@noindent
Output from command
@code{ffe -p html -f FirstName,LastName,Age personnel.fix}
@example
<html>
<head>
</head>
<body>
<table border="1">
<tr>
<th>FirstName</th>
<th>LastName</th>
<th>Age</th>
<tr>
<td>John</td>
<td>Ripper</td>
<td>023</td>
<tr>
<td>Scott</td>
<td>Tiger</td>
<td>045</td>
<tr>
<td>Mary</td>
<td>Moore</td>
<td>041</td>
<tr>
<td>Ridge</td>
<td>Forrester</td>
<td>031</td>
</table>
</body>
</html>
@end example
@subheading Using expression
Printing only Scott's record using expression with previous example:
@code{ffe -p html -f FirstName,LastName,Age -e FirstName^Scott personnel.fix}
@example
<html>
<head>
</head>
<body>
<table border="1">
<tr>
<th>FirstName</th>
<th>LastName</th>
<th>Age</th>
<tr>
<td>Scott</td>
<td>Tiger</td>
<td>045</td>
</table>
</body>
</html>
@end example
@subheading Using replace
Make all bosses and write a new personnel file printing the fields in fixed length format
using directive @code{%D}:
@noindent
Output definition:
@example
output fixed
@{
data "%D"
@}
@end example
@noindent
Write a new file:
@example
$ffe -p fixed -r EmpType=B -o personnel.fix.new personnel.fix
$cat personnel.fix.new
H2006-02-25
BJohn Ripper 023
BScott Tiger 045
BMary Moore 041
BRidge Forrester 031
T0004
$
@end example
@subheading Using constant
The length of the fields FirstName and LastName in fixed length format will be made two bytes longer.
This will be done by printing a constant after those two fields.
We use dots instead of spaces in order to make change more visible.
Because we do not want to change header and trailer we need specially crafted configuration file.
Employee and boss records will be printed using new output @var{fixed2} and other records will be printed using
output @var{default}.
New definition file @file{new_fixed.rc}:
@example
const 2dots ".."
structure personel_fix @{
type fixed
record header @{
id 1 H
field type 1
field date 10
@}
record employee @{
id 1 E
field EmpType 1
field FirstName 9
field LastName 13
field Age 2
output fixed2
@}
record boss @{
id 1 B
fields-from employee
output fixed2
@}
record trailer @{
id 1 T
field type 1
field count 4
@}
@}
output default
@{
data "%D"
@}
output fixed2
@{
data "%D"
field-list Emptype,FirstName,2dots,LastName,2dots,Age
@}
@end example
@noindent
Print new flat file:
@example
$ ffe -c new_fixed.rc personel_fix
H2006-02-25
EJohn ..Ripper ..023
BScott ..Tiger ..045
EMary ..Moore ..041
ERidge ..Forrester ..031
T0004
$
@end example
@subheading Using lookup table
Lookup table is used to explain the EmpTypes contents in output format @code{nice}:
@noindent
Lookup definition:
@example
lookup Type
@{
search exact
pair H Header
pair B "He is a Boss!"
pair E "Not a Boss!"
pair T Trailer
default-value "Unknown record type!"
@}
@end example
@noindent
Mapping the EmpType field to lookup:
@example
structure personel_fix @{
type fixed
record header @{
id 1 H
field type 1
field date 10
@}
record employee @{
id 1 E
field EmpType 1 Type
field FirstName 9
field LastName 13
field Age 2
@}
record boss @{
id 1 B
fields-from employee
@}
record trailer @{
id 1 T
field type 1
field count 4
@}
@}
@end example
@noindent
Adding the lookup option to output definition @code{nice}.
@example
output nice @{
record_header "%s - %r - %f - %o\n"
data "%n=%t\n"
lookup "%n=%t (%l)\n"
justify =
indent " "
@}
@end example
@noindent
Running ffe:
@example
$ffe -p nice personnel.fix
personel_fix - header - personel_fix - 1
type=H
date=2006-02-25
personel_fix - employee - personel_fix - 2
EmpType=E (Not a Boss!)
FirstName=John
LastName=Ripper
Age=023
personel_fix - boss - personel_fix - 3
EmpType=B (He is a Boss!)
FirstName=Scott
LastName=Tiger
Age=045
personel_fix - employee - personel_fix - 4
EmpType=E (Not a Boss!)
FirstName=Mary
LastName=Moore
Age=041
personel_fix - employee - personel_fix - 5
EmpType=E (Not a Boss!)
FirstName=Ridge
LastName=Forrester
Age=031
personel_fix - trailer - personel_fix - 6
type=T
count=0004
@end example
@subheading External lookup file
In previous example the lookup data could be read from external file like:
@example
$cat lookupdata
H;Header
B;He is a Boss!
E;Not a Boss!
T;Trailer
$
@end example
@noindent
Lookup definition using file above:
@example
lookup Type
@{
search exact
file lookupdata
default-value "Unknown record type!"
@}
@end example
@subheading Making universal csv reader using command substitution
Command substitution can be used to make a configuration for reading any csv file.
The number of fields will be read from the first file using awk.
Input file names and date are printed in the file header:
@example
structure csv @{
type separated ,
header first
record csv @{
field-count `awk "-F," 'FNR == 1 @{print NF;exit;@}' $FFE_FIRST_FILE`
@}
@}
output default @{
file_header "Files: `echo $FFE_FILES`\n`date`\n"
data "%n=%d\n"
justify =
@}
@end example
@subheading Reading binary data
A binary block having a 3 byte text (ABC) in 5 bytes long space, one byte integer (35), a 32 bit integer (12345678), a double (345.385), a 3 byte bcd number (45112) and a 4 byte hexadecimal data (f15a9188) can be read using following configuration:
@example
structure bin_data
@{
type binary
record b
@{
field text 5
field byte_int int8
field integer int
field number double
field bcd_number bcd_be_3
field hex hex_be_4
@}
@}
output default
@{
data "%n = %d (%h)\n"
@}
@end example
The @code{%h} directive gives a hex dump of the input data.
Hexadecimal dump of the data:
@example
$ od -t x1 example_bin
0000000 41 42 43 00 08 23 4e 61 bc 00 5c 8f c2 f5 28 96
0000020 75 40 45 11 2f f1 5a 91 88
0000031
@end example
Using ffe:
@example
$ffe -c example_bin.fferc -s bin_data example_bin
text = ABC (x41x42x43x00x08)
byte_int = 35 (x23)
integer = 12345678 (x4ex61xbcx00)
number = 345.385000 (x5cx8fxc2xf5x28x96x75x40)
bcd_number = 45112 (x45x11x2f)
hex = f15a9188 (xf1x5ax91x88)
@end example
Note that the text has only 3 characters before NULL byte. Because this example was made in little endian
machine, same result can be achieved with different configuration:
@example
structure bin_data
@{
type binary
record b
@{
field text 5
field byte_int int8
field integer int32_le
field number double_le
field bcd_number bcd_be_3
field hex hex_be_4
@}
@}
@end example
This configuration is more portable in case the same data is to be read in a different architecture because endianess of integer and double are explicit given.
If the bcd number is read with @code{bcd_le_3} it would look as
@example
bcd_number = 5411 (x45x11x2f)
@end example
Note that nybbles are swapped and last byte is handled as @code{f2} (@code{f} stops the printing) causing only first two bytes to be printed.
and if hexadecimal data is read with @code{hex_le_4} it would look as
@example
hex = 88915af1 (xf1x5ax91x88)
@end example
Bytes are printed starting from the end of the data.
@subheading Printing nested XML
The keyword @code{level} in record definition can be used to print data in multi-level nested form. In this
example a parent row is in level one and a child row is in level two. Children after a parent row belongs
to the parent before child rows, so they are enclosed in a parent element.
Example data:
@example
P,John Smith,3
C,Kathren,6,Blue
C,Jimmy,4,Red
C,Peter,2,Green
P,Margaret Eelers,2
C,Aden,16,White
C,Amanda,20,Black
@end example
A parent row consists of ID (P), parent name, and the count of the children. A child row consists of id (C), child name, age and favorite color.
This can be printed in nested XML using rc file:
@example
structure family
@{
type separated ,
record parent
@{
id 1 P
field FILLER
field Name
field Child_count
level 1 parent
@}
record child
@{
id 1 C
field FILLER
field Name
field Age
field FavoriteColor
level 2 child children
@}
@}
output nested_xml
@{
file_header "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
data "<%n>%t</%n>\n"
indent " "
record_trailer ""
group_header "<%g>\n"
group_trailer "</%g>\n"
element_header "<%m>\n"
element_trailer "</%m>\n"
@}
@end example
Output:
@example
<?xml version="1.0" encoding="UTF-8"?>
<parent>
<Name>John Smith</Name>
<Child_count>3</Child_count>
<children>
<child>
<Name>Kathren</Name>
<Age>6</Age>
<FavoriteColor>Blue</FavoriteColor>
</child>
<child>
<Name>Jimmy</Name>
<Age>4</Age>
<FavoriteColor>Red</FavoriteColor>
</child>
<child>
<Name>Peter</Name>
<Age>2</Age>
<FavoriteColor>Green</FavoriteColor>
</child>
</children>
</parent>
<parent>
<Name>Margaret Eelers</Name>
<Child_count>2</Child_count>
<children>
<child>
<Name>Aden</Name>
<Age>16</Age>
<FavoriteColor>White</FavoriteColor>
</child>
<child>
<Name>Amanda</Name>
<Age>20</Age>
<FavoriteColor>Black</FavoriteColor>
</child>
</children>
</parent>
@end example
@subheading Some examples put in a single file
@example
structure personel_fix @{
type fixed
record header @{
id 1 H
field type 1
field date 10
@}
record employee @{
id 1 E
field EmpType 1 Type
field FirstName 9
field LastName 13
field Age 2
@}
record boss @{
id 1 B
fields-from employee
@}
record trailer @{
id 1 T
field type 1
field count 4
@}
@}
structure personel_sep @{
type separated ,
record header @{
id 1 H
field type
field date
@}
record employee @{
id 1 E
field type
field FirstName
field LastName
field Age
@}
record boss @{
id 1 B
fields-from employee
@}
record trailer @{
id 1 T
field type
field count
@}
@}
structure bin_data
@{
type binary
record b
@{
field text 5
field byte_int int8
field integer int32_le
field number double_le
field bcd_number bcd_be_3
field hex hex_be_4
@}
@}
output xml @{
file_header "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
data "<%n>%t</%n>\n"
record_header "<%r>\n"
record_trailer "</%r>\n"
indent " "
@}
output sql @{
file_header "delete table boss;\ndelete table employee;\n"
record_header "insert into %r values("
data "'%t'"
separator ","
record_trailer ");\n"
file_trailer "commit\nquit\n"
no-data-print no
field-list FirstName,LastName,Age
@}
output nice @{
record_header "%s - %r - %f - %o\n"
data "%n=%t\n"
lookup "%n=%t (%l)\n"
justify =
indent " "
@}
output html @{
file_header "<html>\n<head>\n</head>\n<body>\n<table border=\"1\">\n<tr>\n"
header "<th>%n</th>\n"
record_header "<tr>\n"
data "<td>%t</td>\n"
file_trailer "</table>\n</body>\n</html>\n"
no-data-print no
@}
output fixed
@{
data "%D"
@}
lookup Type
@{
search exact
pair H Header
pair B "He is a Boss!"
pair E "Not a Boss!"
pair T Trailer
default-value "Unknown record type!"
@}
@end example
@subheading Anonymization
Anonymize fields FirstName, LastName and Age for personnel data:
@example
anonymize personnel
@{
method FirstName HASH 2
method LastName HASH 2
method Age NRANDOM
@}
@end example
Data before anonymization:
@example
$cat personnel.fix
H2006-02-25
EJohn Ripper 23
BScott Tiger 45
EMary Moore 41
ERidge Forrester 31
T0004
@end example
Anonymize the data to new file @file{personnel_anon.fix} (using the default configuration file @file{~/.fferc} and raw output):
@example
ffe -A personnel -praw -o personnel_anon.fix personnel.fix
@end example
Anonymized data:
@example
$cat personnel_anon.fix
H2006-02-25
EJQIQ9C5oBR2rDU0qiSTv7E62
BSqUcsYzSTTNTuTraspsG4154
EMTsXkHltVMsV8qmK1tkgq 00
ER1e90zv1dFjP4 xgflVGQF87
T0004
$ffe -pnice personnel_anon.fix
personel - header - personnel_anon.fix - 1
type=H
date=2006-02-25
personel - employee - personnel_anon.fix - 2
EmpType=E
FirstName=JQIQ9C5oB
LastName=R2rDU0qiSTv7E
Age=62
personel - boss - personnel_anon.fix - 3
EmpType=B
FirstName=SqUcsYzST
LastName=TNTuTraspsG41
Age=54
personel - employee - personnel_anon.fix - 4
EmpType=E
FirstName=MTsXkHltV
LastName=MsV8qmK1tkgq
Age=00
personel - employee - personnel_anon.fix - 5
EmpType=E
FirstName=R1e90zv1d
LastName=FjP4 xgflVGQF
Age=87
personel - trailer - personnel_anon.fix - 6
type=T
count=0004
@end example
FirstName and LastName have preserved the first letter because anonymization started from the second byte. Age is a two digit random number.
Name fields will get the same anonymized value for each run, but Age will have a random value for each run.
@subheading Using @command{ffe} to test file integrity
@command{ffe} can be used to check flat file integrity, because @command{ffe}
checks for all lines the line length and id's for fixed length structure
and field count and id's for separated structure.
@noindent
Integrity can be checked using command
@code{ffe -p no -l inputfiles@dots{}}
@noindent
Because option @option{-p} has value @code{no} nothing is printed to output except the error messages.
Option @option{-l} causes all erroneous lines to be reported, not just the first one.
@noindent
Example output:
@example
ffe: Invalid input line in file 'inputfileB', line 14550
ffe: Invalid input line in file 'inputfileD', line 12
@end example
@node Problems, , ffe configuration, Top
@chapter Reporting Bugs
@cindex bugs
@cindex problems
If you find a bug in @command{ffe}, please send electronic mail to
@email{tjsa@@iki.fi}. Include the version number, which you can find by
running @w{@samp{ffe --version}}. Also include in your message the
output that the program produced and the output you expected.@refill
If you have other questions, comments or suggestions about
@command{ffe}, contact the author via electronic mail to
@email{tjsa@@iki.fi}. The author will try to help you out, although he
may not have time to fix your problems.
@contents
@bye
|