This script was modified from a weblog,
https://blog.youkuaiyun.com/moyu123456789/article/details/86738718
#!/bin/bash
# filter topic list, just key word
filter_topic_key_words=("car" "localization")
#1.get the info of the bag
if [ $# != 2 ]; then
echo "the input params number is not 2,please try again!"
exit 1
fi
rosbag info $1 > bag.txt
#2.get the delete topics form the bag
bag_file=./bag.txt
for key_word in ${filter_topic_key_words[@]}
do
for line in $(grep "/$key_word/" $bag_file)
do
result=$(echo $line | grep "/$key_word/")
if [[ "$result" != "" ]]
then
echo $line >> topics.txt
fi
#echo $line >> topics.txt
done
done
topic_file=./topics.txt
if [ ! -s $topic_file ]
then
echo "there is no specify topic in the $1 bag."
read
exit 1
fi
#3.create the commands to delete the topics
line_count=0
topicinfo=""
for line in $(cat $topic_file)
do
if [ 0 == $line_count ];then
topicinfo="topic!='$line'"
else
topicinfo=$topicinfo" and topic!='$line'"
fi
line_count=$line_count+1
done
echo "rosbag filter $1 $2 ""\""$topicinfo"\"" > rosbag.sh
#4.execute the cmd
chmod 777 rosbag.sh
./rosbag.sh
echo "###########the new bag $2 has created now#############"
#5.delete the files no need
rm -rf bag.txt
rm -rf topics.txt
rm -rf rosbag.sh