Skip to main content
Logo

Bash And Exclusions in a List

February 4, 2014
1 min read

Just a quick snippet on doing exclusions when you loop through a list.

DIRS=`ls -l --time-style="long-iso" $MYDIR | egrep '^d' | awk '{print $8}'`
EXCLUDELIST="mail Mail"
for EXCLUDE in $EXCLUDELIST
do
DIRS=`echo $DIRS | sed "s/\b$EXCLUDE\b//g"`
done
for DIR in $DIRS
do
echo ${DIR} :
done

For some reason on Solaris sed had an issue with “\b” so I adjusted to sed “s/$EXCLUDE//g”. Shown as follow:

#Linux:
$ echo "d1 d2 d3" | sed "s/\bd2\b//g"
d1 d3
# Solaris Fails:
# echo "d1 d2 d3" | sed "s/\bd2\b//g"
d1 d2 d3
# Solaris Works
# echo "d1 d2 d3" | sed "s/d2//g"
d1 d3