search and delete a line

Folks,

I have a set of list that contains file names. I want to search through each of the list and delete any file that is found in the list.

eg.
LIST_A contains:
aaa
bbb
ccc
ddd
eeee

LIST_B contains:
aab
aac
adb
aed

assuming I want to delete 'aac' from the list and I don't know which of the list contains 'aac', what's the best way to search through the list and delete the file if found?

This is what i've tried
ans=`cat LIST_A | grep aac`
if [[ $ans = "" ]]
then
echo "Not found in LIST_A"
ans2=`cat LIST_B | grep aac`
if [[ $ans2 = ""]]
then
echo "Not founf in LIST_B"
else
** delete the line (How will I code this portion)**
fi
else
** delete the line (How will I code this portion)**
fi

I will appreciate whatever assistance I can get from you guys.

Thanks,

Odogbolu98:confused:

Hi Odogbolu98

try

while read line
do
if [ "$line" = "acc" ]
then
echo "acc found and removed"
else
echo $line >> new_LIST_A
fi
done < LIST_A

while read line
do
if [ "$line" = "acc" ]
then
echo "acc found and removed"
else
echo $line >> new_LIST_B
fi
done < LIST_B

You will get new_LIST_A and new_LIST_B without the offending lines!

Hope this helps
Helen :slight_smile:

This can be done

for i in files
do
grep -vw "aac"<$i>$i.new
mv $i.new $i
done:D

Can you clarify if you want files deleting or if you want lines within your list of filenames removing?

Cheers
Helen

Thanks Bab00shka & ganti:

Bab00shka: In answering to your question:

Actually what I wanted it to remove the entire line where the string is found. So it's like deleting the whole line. I'll give both suggested solution a trial.

Thanks,

Odogbolu98

You could use sed

sed '/acc/d' LIST_A > LIST_A.tmp
mv LIST_A.tmp > LIST_A

sorry, bit of a typo. That should be

sed '/acc/d' LIST_A > LIST_A.tmp
mv LIST_A.tmp LIST_A