[Tex/LaTex] Getting certain data out of .txt file

external filespgfplotstabletables

I'd like to use an external .txt file to provide a table in my LaTeX document with data. To achieve this I've taken into consideration the pgfplotstable package and the filecontents package. Both of them enabled me to create a complete table out of the data (still, only after manually removing the [] brackets from my .txt file). But I would like to take only certain data and insert them into an existing table of my document.

The .txt file has [] brackets in it, that could be used as markers for a new entry line?

Example:

\documentclass{scrartcl}
\usepackage{tabularx}
\usepackage[table]{xcolor} 
\usepackage{pgfplotstable, filecontents}

\begin{filecontents*}{data.txt}
Title Data
[Name] Clarissa
[Family Name] Fray
[Age] 16
[Country] USA
[Gender] Female
[Haircolor] Red
\end{filecontents*}

\begin{document} 

%% --------------------- What it should look like (but getting data from file)
\begin{figure}
    \rowcolors{1}{gray!25}{white}
        \begin{tabularx}{\textwidth}{|>{\bfseries}l|X|} \hline
        Name & Clarissa \\ \hline
        Family Name & Fray \\ \hline
        Gender & Female \\ \hline
        Age & 16 \\ \hline
    \end{tabularx}
\end{figure}

%%% --------------------- The best I can get (doesn't compile, because of
%%% the brackets in the .txt file and the space in [Family Name])
%\pgfplotstabletypeset[
%every head row/.style = {
%   before row = {
%       \hline
%       }
%   }, 
%after row = \hline,
%every even row/.style={
%   before row = {
%       \rowcolor[gray]{0.9}
%       }
%},
%columns/Title/.style = {column type=|l, column name=First},
%columns/Data/.style = {column type=|l|, column name=Second},
%col sep = space,
%string type,
%]{data.txt}

\end{document}

Any help is really appreciated, thank you!

Best Answer

On a normal Windows 10 PC, the following should work from a PowerShell prompt. It may also work on Windows 7 or 8, but I can't test that easily right now:

cat .\data.txt | select -skip 1 | % { $_ -replace "\[", "" } | % { $_ -replace "\]", "," } | Out-File -encoding ascii data2.txt

Annotated version:

  • cat .\data.txt gets all the content of data.txt and sends it to the next command in the pipeline
  • | select -skip 1 skips the first line (your headers) from the piped input
  • | % { $_ -replace "[", "" } replaces open bracket characters with empty strings
  • % { $_ -replace "]", "," } replaces close bracket characters with commas
  • | Out-File -encoding ascii data2.txt writes the pipeline data into a file data2.txt in plan ASCII format

This yields a file data2.txt containing:

Name, Clarissa
Family Name, Fray
Age, 16
Country, USA
Gender, Female
Haircolor, Red

and with a document of:

\documentclass{scrartcl}
\usepackage[table]{xcolor} 
\usepackage{pgfplotstable}

\begin{document} 

\pgfplotstabletypeset[
every head row/.style = {
   before row = {
       \hline
       }
   }, 
after row = \hline,
every even row/.style={
   before row = {
       \rowcolor[gray]{0.9}
       },
},
columns/Title/.style = {column type=|l, column name=First},
columns/Data/.style = {column type=|l|, column name=Second},
col sep = comma,
string type,
]{data2.txt}

\end{document}

you get output of:

enter image description here

Related Question