How to remove common values of one list from another using variables

Viewed 45

I have a multiple values stored in a variable (directory names). To view values I put them into a lst file.

for i in ' */ ' ; do
  echo $i > list.lst
done

###Turn row into column
fmt -1 list.lst > listColumns.lst
sed 's:/::g' listColumns.lst > predicted.lst


for data in $(cat predicted.lst) ; do
  predicted=$data
  #echo $predicted
done

###Get names of targets and assign to variable $targets
for names in $(cat targets.lst) ; do
  targets=$names
  #echo $targets
done



###Remove contents of $predicted from $targets and put into new list
grep -v "${predicted}" "${targets}" > targets2.lst

For the last part of actually removing $predicted from $targets I also tried using sed but with no luck.

Thanks for any help.

1 Answers
  • for i in ' */ ' does not expand to directory names. You should not quote */.
  • echo $i > list.lst will overwrite existing file and the last element only remains. Besides you need to quote "$i".
  • predicted=$data will overwrite existing variable.

If you want to exclude existing directory names from targets.lst, please try instead:

grep -Fxvf <(ls -d -1 */ | tr -d /) targets.lst
  • The -f option to grep takes the 1st argument as a filename which includes the patterns.
  • The -Fx option assures the exact match of the strings.
  • <(command ..) is a process substitution behaving as if a file.
  • ls -d -1 */ prints directory names one per line.
  • tr -d / removes the slashes.

[Edit]
If the process substitution does not work well for some reason, please try instead:

ls -d -1 */ | tr -d / | grep -Fxvf - targets.lst

where grep -f - specifies the standard input passed via the pipe as the pattern.

Related