I have a find script that automatically opens a file if just one file is found. The way I currently handle it is doing a word count on the number of lines of the search results. Is there an easier way to do this?
if [ "$( cat "$temp" | wc -l | xargs echo )" == "1" ]; then
edit `cat "$temp"`
fi
EDITED - here is the context of the whole script.
term="$1"
temp=".aafind.txt"
find src sql common -iname "*$term*" | grep -v 'src/.*lib' >> "$temp"
if [ ! -s "$temp" ]; then
echo "ø - including lib..." 1>&2
find src sql common -iname "*$term*" >> "$temp"
fi
if [ "$( cat "$temp" | wc -l | xargs echo )" == "1" ]; then
# just open it in an editor
edit `cat "$temp"`
else
# format output
term_regex=`echo "$term" | sed "s%\*%[^/]*%g" | sed "s%\?%[^/]%g" `
cat "$temp" | sed -E 's%//+%/%' | grep --color -E -i "$term_regex|$"
fi
rm "$temp"
$temp
a real file containing filenames or a variable with one or more filenames? – Pinnatifidxargs echo
? Are you trying to get rid of the spaces around the output ofwc
? Just get rid of the double quotes around$(...)
. – Treble$temp
is or how you go about finding the files. – Stonemasonfind ... | grep -v
is silly -- you can just tell find to prune things you don't want.find src sql common -path 'src/*lib' -prune -o -iname "*$term*" -print
doesn't need any grepping. – Automotive