Skip to content

Bash and exclusions in a list

wordpress meta

title: 'Bash And Exclusions in a List'
date: '2014-02-05T08:22:44-06:00'
status: publish
permalink: /bash-and-exclusions-in-a-list
author: admin
excerpt: ''
type: post
id: 538
category:
    - Bash
tag: []
post_format: []

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