Skip to content

Instantly share code, notes, and snippets.

@plusk01
Created May 3, 2018 23:23
Show Gist options
  • Save plusk01/1864ae3cfcec43ef8f7ef6e121c08df0 to your computer and use it in GitHub Desktop.
Save plusk01/1864ae3cfcec43ef8f7ef6e121c08df0 to your computer and use it in GitHub Desktop.
BASH function to split ROS bags
# Use rosbag filter to create a new bag
#
# $ bagcut original.bag 0 336 # cuts from 0 to 336s
#
# $ bagcut original.bag 50 # new bag starts 50s in
#
# $ bagcut original.bag 10 -20 # cuts from 10 to END_TIME-20s
function bagcut {
BAG=${1:0:-4}
OFFSET=$2
START=`rosbag info ${BAG}.bag | grep start | head -1 | tr -s ' ' | cut -d " " -f 6 | bc`
END=`rosbag info ${BAG}.bag | grep end | head -1 | tr -s ' ' | cut -d " " -f 6 | bc`
NEWSTART=`echo "$START + $OFFSET" | bc`
# Is there an ending limit?
if [ ! -z "$3" ]
then
if [ `echo "$3 < 0" | bc` -ne "0" ]
then
END=`echo "$END - $3" | bc`
else
END=`echo "$START + $3" | bc`
fi
fi
rosbag info ${BAG}.bag | grep start | head -1
rosbag info ${BAG}.bag | grep end | head -1
set -x
rosbag filter ${BAG}.bag ${BAG}.split.bag "t.secs >= $NEWSTART and t.secs <= $END"
{ set +x; } 2>/dev/null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment