Get WordPress Multisite blog_id by blog's path using wp-cli

You have a csv file has this format (mycsv.csv):

path,some_field
somepath,some_value
anotherpath,another_value
...

and you want to get the blog_ids of those path(s). This following bash shell will help you to get blog_ids by taking advantages of wp-cli, get_blogid_from_url.sh:

#! /bin/bash
# example csv file, mycsv.csv:
#
# path,some_field
# somepath,some_value
# anotherpath,another_value
# ...
#
# run the following command:
# ./get_blogid_from_url.sh mycsv.csv myblog.com /wordpress/path > result.csv
#
# get network's blog list in csv format
sites=$(wp site list --allow-root --path=$3 --fields=blog_id,url --format=csv)
# parse the input csv file
IFS=','
while read a1 a2
do
# parse the network's blog list
echo "$sites" | while read f1 f2
do
# remove the carriage return from the last column
blog_url=$(echo $f2 | sed -e 's/\r//g')
if [ $blog_url == "$2/$a1/" ]
then
echo "$f1,$blog_url"
fi
done
done < $1


$ chmod +x get_blogid_from_url.sh
$ ./get_blogid_from_url.sh mycsv.csv myblog.com /wordpress/path > result.csv

The result will be something like:

546,myblog.com/somepath/
456,myblog.com/anotherpath/
...