bobbyiliev / introduction-to-bash-scripting

Free Introduction to Bash Scripting eBook
https://ebook.bobby.sh
MIT License
4.39k stars 451 forks source link

Help: a variable list filtering via sed + "blacklist-variable" #59

Closed blackcrack closed 2 years ago

blackcrack commented 2 years ago

dirlist=$(ls /home) =>

dirlist="blackcrack akira lost&found workshop garage office" blacklist="lost&found garage workshop"

the way to the result echo $result blackcrack akira office

how become from the $dirlist i a filtering with the 3 words in the $blacklist to a result with the rest of the words..

i have try it via oneliner :

[blackcrack@blackysgate ~/bearbeit-net]$ dirlist=$(ls /home) ; blacklist="lost&found garage workshop" ; echo $dirlist | sed 's/$blacklist//'
blackcrack akira lost&found workshop garage office
[blackcrack@blackysgate ~/bearbeit-net]$

but it works not really simply with sed.. mus i use there a loop or something over "while"

best

PsypherPunk commented 2 years ago

Presuming your variables are as above:

dirlist="blackcrack akira lost&found workshop garage office"
blacklist="lost&found garage workshop"

If you turn them into arrays then yes, you can exclude one from the other:

dirlist_array=(${dirlist})
blacklist_array=(${blacklist})

for x in ${blacklist_array[@]}
do
    dirlist_array=("${dirlist_array[@]/$x}")
done

That will give you:

$ echo ${dirlist_array[@]}
blackcrack akira office

Is that of any use?