You are not logged in.
I need to have sed read in a file (single column of words) and remove any that are on a blacklist as defined in an array. I'm kinda there but I don't know how to do process the entire array and THEN write out the resulting "censored" file.
Example input list (let's call the file 'foo'):
corn
romaine
arugula
keyboard
carrot
yogurt
peas
Here is the bash script that only works when there is exactly one word in the ignore array:
#!/bin/bash
ignore=(keyboard yogurt)
for i in ${ignore}; do
sed -e "s/$i//g" -e '/^$/d' foo>bar
done
The flaw in my design here I think is that I keep overwriting 'bar' rather than doing all the processing, then writing out 'bar'. Suggestions are welcomed - I'm always glad to learn and thanks in advance.
Last edited by graysky (2010-10-24 10:03:37)
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
Are you sure this is not homework?
Offline
@Allan - Homework... I haven't done homework in more years than I care to recall! I want to implement this feature in my AUR package for modprobed_db. The script within keeps track of kernel modules probed so that users can build a kernel with 'make localmodconfig' and only get the modules they need. The blacklist array will contain modules that get built by packages like virtualbox and nvidia, for example: (nvidia vboxdrv vboxnetflt vboxnetadp) that we don't want probed for the compilation. In the real case, the input file will be /var/log/modprobe.long that my script in that package generates - I only presented the problem in simple "salad" terms to make it easier for the community to see what I want to do
Last edited by graysky (2010-10-24 10:29:47)
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
OK then... had to check
allan@mugen ~
> cat foo.txt
one
two
three
allan@mugen ~
> sed '/^two$/d' foo.txt
one
three
Offline
If you use an array it has to be
for i in ${ignore[*]}; do ...
to iterate over all elements.
Offline
@allan - I can do that part... I just can't parlay the code into working with my array.
@portix - I tried your suggestion but got the same result.
#!/bin/bash
ignore=(keyboard yogurt)
for i in ${ignore[*]}; do
sed -e "s/$i//g" -e '/^$/d' foo>bar
done
So 'bar' still contains the blacklisted word 'keyboard':
corn
romaine
arugula
keyboard
carrot
peas
Last edited by graysky (2010-10-24 10:45:45)
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
after your sed command you may want to move the new file to the old:
for i in.... ; then
sed .... foo > bar
mv bar foo
fi
so you are actually filtering the stuff out
Last edited by olvar (2010-10-24 11:08:30)
Offline
Offline
I would do the filtering with grep -v, using extended regexp.
#!/bin/bash
ignore=(keyboard yogurt)
grep -Ev "`echo ${ignore[*]} | sed 's/ /|/g'`" foo > bar
“First principle, Clarice. Simplicity” – Dr. Hannibal Lecter
Offline
Offline
@barto - thanks for the code! I updated my package accordingly.
Last edited by graysky (2010-10-25 06:56:49)
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
@falconindy - Thanks for your notice, you are right.
Adding the option -x to grep to make it match whole lines should solve the issue.
“First principle, Clarice. Simplicity” – Dr. Hannibal Lecter
Offline