The xstring
package has a macro StrBetween[n,m]{full string}{string A}{string B}
that may help here. It will select the substring between the nth occurrence of string A and the mth occurrence of string B. In this case you can set both string A and string B to be a space and extract the string between the 2nd and 3rd space to get 15 and the string between the 4th and 5th space to get 23.
The second part of the question is to get the external output into a format that xstring
can handle. This is a bit tricky. \splice
is doing a lot under the hood that causes trouble when expanding the macro for string processing. A better alternative is to use \bashStdout
as defined by bashful
that captures the output of the last \bash \END
block as a plain string. The only problem here is that spaces have category code 12 which also confuses xstring
. As a solution you can 'flatten' the string using \detokenize
. This has the added advantage that you can use \edef
which expands the argument, allowing for other \bash \END
blocks (which will overwrite \bashStdout
) in between the execution and the actual use of the values.
MWE:
\documentclass{article}
\usepackage{bashful}
\usepackage{xstring}
\begin{document}
\bash
echo "4 8 15 16 23 42"
\END
\edef\data{\detokenize\expandafter{\bashStdout}}
All data: \data
\begin{itemize}
\item \StrBetween[2,3]{\data}{ }{ } %15
\item \StrBetween[4,5]{\data}{ }{ } %23
\end{itemize}
\end{document}
Result:
bashful
, why not count the occurrences in bash?4 8 15 16 23 42
and that you want to extract data therefrom?--shell-escape
it is not reasonable to ask people to run tex fragments copied from the internet with shell escape enabled