Question

I have a file containing valid xmls (one per line) and I want to execute a utility (xpath) on each line one by one.

I tried xargs but that seems doesn't seem to have an option to pass the line as stdin :-

% cat <xmls-file> | xargs -p -t -L1 xpath -p "//Path/to/node"

Cannot open file '//Path/to/node' at /System/Library/Perl/Extras/5.12/XML/XPath.pm line 53.

I also tried parallel --spreadstdin but that doesn't seem to work either :-

% cat <xmls-file> | parallel --spreadstdin  xpath -p "//Path/to/node"
junk after document element at line 2, column 0, byte 1607
Was it helpful?

Solution

If you want every line of a file to be split off and made stdin for a utility

you could use a for loop in bash shell:

cat xmls-file | while read line
do ( echo $f > /tmp/input$$; 
     xpath -p "//Path/to/node" </tmp/input$$
     rm -f /tmp/input$$
);
done

The $$ appends the process id number, creating a unique name

I assume xmls-file contains, on each line, what you want iterated into $f and that you want this as stdin for a command line, not as a parameter to the command.

On the other hand, your specification may be incorrect and maybe instead you need each line to be part of a command. In that case, delete the echo and rm lines, and change the xpath command to include $f wherever the line from the file is needed.

I've not done much XML so the do command may need to be edited.

OTHER TIPS

You are very close with the GNU Parallel version; only -n1 missing:

cat <xmls-file> | parallel -n1 --spreadstdin  xpath -p "//Path/to/node"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top