Sunday, July 3, 2011

obtain a figure file list from a tex file using grep and sed: new version

#!/bin/bash 

# to get a list of pdf files included in a tex file 
# echo "Usage: $0 {file.name}" 
# echo "Processing file $1"


EXPECTED_ARGS=1
E_BADARGS=65

if [ $# -ne $EXPECTED_ARGS ]
then
  echo "Usage: `basename $0` {tex.file}"
  exit $E_BADARGS
fi

tmpfile=`mktemp -p . exttmp.XXXX`

grep includegraphics $1 > $tmpfile
# sed -e 's/includegraphics/  /' -e 's/[\\{}]//g' -e 's/\[.*\]//g' -e 's/[^ ]\+//' -e 's/^ \+//' < $tmpfile 

# this one is much better than the above 
sed -e 's/.*{\(.*\.pdf\)}.*/\1/' < $tmpfile

if [ -f "$tmpfile" ]
then
    rm "$tmpfile"
fi

tmpfile=`mktemp -p . exttmp.XXXX`

grep includegraphics $1 > $tmpfile
sed -e 's/includegraphics/  /' -e 's/[\\{}]//g' -e 's/\[.*\]//g' -e 's/[^ ]\+//' -e 's/^ \+//' < $tmpfile

if [ -f "$tmpfile" ]
then
    rm "$tmpfile"
fi

#
# the includegraphics must be at the same line with the pdf or other figure files included. 

No comments: