Linux convert dbf to csv

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Small command line utility to convert simple *.DBF files to *.CSV

License

akadan47/dbf2csv

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Small command line utility to convert simple *.DBF files to *.CSV

dbf2csv [-h] [-v] [-ie INPUT_ENCODING] [-oe OUTPUT_ENCODING] [-q ] [-d DELIMITER_CHAR] [-e ESCAPE_CHAR] input [output] small utility to convert simple *.DBF files to *.CSV positional arguments: input output optional arguments: -h, --help show this help message and exit -v, --version show program's version number and exit -ie INPUT_ENCODING, --input-encoding INPUT_ENCODING charset of *.dbf files (default: cp850) -oe OUTPUT_ENCODING, --output-encoding OUTPUT_ENCODING charset of *.csv files (default: utf8) -q , --quoting-mode quoting mode for csv files (default: minimal) -d DELIMITER_CHAR, --delimiter-char DELIMITER_CHAR delimiter char for csv files (default: ",") -e ESCAPE_CHAR, --escape-char ESCAPE_CHAR escape char for csv files (default: "\") 
dbf2csv input.dbf > output.csv # Grep output dbf2csv file.dbf | grep # Convert all *.dbf files from current dir, tab-delimited CSV dbf2csv . output/ -d $'\t' # Convert all *.dbf files from input dir to output dir, with charset conversion, quote all fields dbf2csv /path/to/input/dir/ /path/to/output/dir/ -ie cp866 -oe cp1251 -q all 

About

Small command line utility to convert simple *.DBF files to *.CSV

Читайте также:  Embedded linux for stm32

Источник

Convert DBF to CSV

I have a number of DBF database files that I would like to convert to CSVs. Is there a way to do this in Linux, or in PHP? I’ve found a few methods to convert DBFs, but they are very slow.

3 Answers 3

$ soffice --headless --convert-to csv FILETOCONVERT.DBF 

Change the files variable to a path to your DBF files. Make sure the file extension matches the case of your files.

set_time_limit( 24192000 ); ini_set( 'memory_limit', '-1' ); $files = glob( '/path/to/*.DBF' ); foreach( $files as $file ) < echo "Processing: $file\n"; $fileParts = explode( '/', $file ); $endPart = $filePartsLinux convert dbf to csv; $csvFile = preg_replace( '~\.[a-z]+$~i', '.csv', $endPart ); if( !$dbf = dbase_open( $file, 0 ) ) die( "Could not connect to: $file" ); $num_rec = dbase_numrecords( $dbf ); $num_fields = dbase_numfields( $dbf ); $fields = array(); $out = ''; for( $i = 1; $i $val ) < if( $key == 'deleted' ) continue; if( $firstKey != $key ) $out .= ';'; $out .= trim( $val ); >$out .= "\n"; > file_put_contents( $csvFile, $out ); > 

Using @Kohjah’s code, here an update of the code using a better (IMHO) fputcsv approach:

// needs dbase php extension (http://php.net/manual/en/book.dbase.php) function dbfToCsv($file) < $output_path = 'output' . DIRECTORY_SEPARATOR . 'path'; $path_parts = pathinfo($file); $csvFile = path_parts['filename'] . '.csv'; $output_path_file = $output_path . DIRECTORY_SEPARATOR . $csvFile; if (!$dbf = dbase_open( $file, 0 )) < return false; >$num_rec = dbase_numrecords( $dbf ); $fp = fopen($output_path_file, 'w'); for( $i = 1; $i fputcsv($fp, $row); > fclose($fp); > 

This question is in a collective: a subcommunity defined by tags with relevant content and experts.

Источник

Convert DBF to CSV on Linux with Perl and XBase

I spent a few days to figure out how to make a simple conversion of a DBF file into a plain text file as comma separated values (CSV) or tab separated values (TSV) in a batch/command line way. I was almost setting up an OpenOffice.org server because it seamed to be the only packaged solution to read and convert DBFs.

Well, it easier than that. The secret lives in the XBase drivers for Perl Data Base Interface project. To install it on Fedora or Red Hat use this command:

If you use other Linux distributions, you may look for a package with a similar name.

For a simple file conversion, you don’t need to program in Perl because the perl-DBD-XBase package contains a command line tool for that: dbftool.pl. So to convert the mytable.dbf file into a more standard CSV file, do this:

dbfdump.pl --fs "," mytable.dbf > mytable.csv

If you want to use TAB instead of a comma to separate fields, while editing the command put the cursor between the double quotes, press Ctrl+V and then press the TAB key on your keyboard. This will fill the double quotes with a real (but invisible) TAB char.

Читайте также:  Hello world ассемблер linux

9 thoughts on “Convert DBF to CSV on Linux with Perl and XBase”

I have just happened to work on some dbf dbase files too recently, and find out your post is very helpful (I plan to use perl dbi at the beginning). Here are some extra works I have done if you run windows, or other distributions of linux, such as opensuse, debian, ubuntu… hope is helpful for anyone who meet the same problem. ATTENTION: make sure you use –nomemo switch when you run dbfdump, otherwirse you will get error message (missing dbt). WINDOWS XP: install activeperl, and then install dbd-xbase package under ppm-shell. then run dbfdump.bat (mine is under c:\perl\site\bin) dbfdump.bat –fs=:|” –nomemo mydebasefile.dbf LINUX, install perl-dbd-xbase (suse), libdbd-xbase-perl (debian or ubuntu), and run dbfdump.bat –fs=:|” –nomemo mydebasefile.dbf for some version incompatible reason, you may get the error message under linux prototype mismatch: sub Xbase::Base::O_BINARY () vs none at (eval 4). what I did is find the Fcntl.pm file for perl, and comment out the O_BINARY line, it works. If you need more information, google could help you more.

Commented out O_BINARY as suggested [mlapier@mushroom]$ dbfdump.pl –fs “,” 20110205dailydat.dbf > 20110205dailydat.csv
Possible attempt to put comments in qw() list at /usr/lib/perl5/Fcntl.pm line 140.
Prototype mismatch: sub XBase::Base::O_BINARY () vs none at (eval 5) line 1.
Constant subroutine O_BINARY redefined at (eval 5) line 1.
[mlapier@mushroom]$ I’m open for suggestions? 😉

I have no idea. I used it only once or twice. I would guess that your DBF has binary fields which are incompatible with such conversion because CSV is text only. If so, I would guess you need to use the XBase Perl API instead of using the vanilla dump tool. Another guess is the encoding of text on your DBF file is incompatible with your environment so the tool is interpreting it as binary. But again, these are only guesses.

Thanks, I have been wondering for years how to insert data from a .dbf into samba and linux server, this has helped considerably. Next step, using diff to check for changes and then to use sed or gawk to take certain fields from the changes for input into a lazy-admin-tools input file. I might need to use read/readline too.
Learning is fun.

Some work still needs to be done on the intelligent checking of input fields but so far I have tested the following and it worked.
Thanks again to this site, and linuxcommand.org, and man and info pages

# warning! Script needs intelligence # use dbfdump to convert DB2000 student database to comma-seperated values # find the new or changed student entries and put in a list of changes # replace the known student list # rip out and reorder the fields from the changes list into a file for lat-users # rip out and add user names to filter list to allow internet access then reload danguardian filter groups # run lazy admin tools to import data into sme-server samba and linux users # Update samba domain group maps dbfdump --fs "," /home/deltacopy/db2000/MC$(date +%Y)/Student.dbf > /root/students.new diff --old-line-format='' --unchanged-line-format='' --new-line-format='%L' /root/students.old /root/students.new > /root/students.changed mv students.new students.old -f gawk 'BEGIN < FS = "," ; OFS = "|" >\ < print "s"$1,$4,$3,$2,"","","","","","","","","students","power" >"/root/newstudents.csv" >' /root/students.changed gawk 'BEGIN < FS = "," ; OFS = "=" >\ < print "s"$1,"filter2 # students" >>"/etc/danguardian/lists/filtergroupslist" >' /root/students.changed dansguardian -g lat-users -a -i /root/newstudents.csv &&\ rm -rf /root/newstudents.csv &&\ /etc/e-smith/events/actions/update-domain-group-maps

Thanks wintel. Used the dbf_dump command from the libdbd-xbase-perl package on Ubuntu to good effect.

Читайте также:  Apache php config linux

I’m on Ubuntu 14.04 LTS. I installed libdbd-xbase-perl via Synaptic. Then trying to do dbfdump, I got the following:
snoopie2@snoopie2-Compaq-Presario-CQ40-Notebook-PC:~/special-tools/sage-6.2-i686-Linux$ dbfdump
程序“dbfdump”尚未安装。 您可以使用以下命令安装:
sudo apt-get install shapelib
snoopie2@snoopie2-Compaq-Presario-CQ40-Notebook-PC:~/special-tools/sage-6.2-i686-Linux$ sudo apt-get install shapelib[sudo] password for snoopie2:
正在读取软件包列表… 完成
正在分析软件包的依赖关系树
正在读取状态信息… 完成
将会安装下列额外的软件包:
libshp1
下列【新】软件包将被安装:
libshp1 shapelib
升级了 0 个软件包,新安装了 2 个软件包,要卸载 0 个软件包,有 30 个软件包未被升级。
需要下载 59.2 kB 的软件包。
解压缩后会消耗掉 217 kB 的额外空间。
您希望继续执行吗? [Y/n] y
获取:1 http://hk.archive.ubuntu.com/ubuntu/ trusty/universe libshp1 i386 1.2.10-7 [22.8 kB]
获取:2 http://hk.archive.ubuntu.com/ubuntu/ trusty/universe shapelib i386 1.2.10-7 [36.4 kB]
下载 59.2 kB,耗时 1秒 (40.1 kB/s)
Selecting previously unselected package libshp1:i386.
(正在读取数据库 … 系统当前共安装有 237520 个文件和目录。)
Preparing to unpack …/libshp1_1.2.10-7_i386.deb …
Unpacking libshp1:i386 (1.2.10-7) …
Selecting previously unselected package shapelib.
Preparing to unpack …/shapelib_1.2.10-7_i386.deb …
Unpacking shapelib (1.2.10-7) …
Processing triggers for man-db (2.6.7.1-1) …
正在设置 libshp1:i386 (1.2.10-7) …
正在设置 shapelib (1.2.10-7) …
Processing triggers for libc-bin (2.19-0ubuntu6) …
snoopie2@snoopie2-Compaq-Presario-CQ40-Notebook-PC:~/special-tools/sage-6.2-i686-Linux$ dbfdump
dbfdump [-h] [-r] [-m] xbase_file
-h: Write header info (field descriptions)
-r: Write raw field info, numeric values not reformatted
-m: Multiline, one line per field.
snoopie2@snoopie2-Compaq-Presario-CQ40-Notebook-PC:~/special-tools/sage-6.2-i686-Linux$ man dbfdump
snoopie2@snoopie2-Compaq-Presario-CQ40-Notebook-PC:~/special-tools/sage-6.2-i686-Linux$
It turned out you need to install shapelib to get dbfdump in Ubuntu 14.04.

Hi, I installed and used this but the problem I am facing it is that even though the dbf file is converted to csv file it does it without having the header. Meaning the csv file does not have the header that the dbf file was having. How can we resolve this? Thanks,
Binish

Leave a Reply Cancel reply

About Avi

photo

Avi Alkalay is the lead architect for Third Platform solutions (mobile, user engagement, analytics, cloud, security) on the IBM Brazil CAMSS team.

Categories

  • Blog do Papai (10)
  • Chronicles (71)
  • Community and Society (150)
    • Consumo Consciente (14)
    • Urbanism (6)
    • Cloud (6)
    • Data Science (13)
    • Linux & Open Source (127)
      • Linux Journal Index (14)
      • Podcast: brazilian jazz (11)
      • Podcast: general (15)
      • Amazônia 2023 (6)
      • Central Asia 2007 (29)

      Источник

Оцените статью
Adblock
detector