Introduction
In this project, you will learn how to extract image URLs from Markdown files using a Bash script. This is a common task when working with technical documentation, as it allows you to quickly identify and retrieve the images used in a document.
👀 Preview
$ ./getimage.sh labex_lab1.md
https://doc.shiyanlou.com/document-uid13labid292timestamp14677222211211.png
https://doc.shiyanlou.com/document-uid13labid292timestamp14672311234511.png
https://doc.shiyanlou.com/document-uid13labid292timestamp14677029556772.png
🎯 Tasks
In this project, you will learn:
- How to create a Bash script to extract image URLs from a Markdown file
- How to make the script executable and run it from the command line
- How to customize the script to save the extracted URLs to a file
🏆 Achievements
After completing this project, you will be able to:
- Automate the process of extracting image URLs from Markdown files
- Incorporate this script into your workflow when working with technical documentation
- Customize the script to suit your specific needs and requirements
Extract Image URLs From Markdown File
In this step, you will learn how to extract all image URLs from a Markdown file using a Bash script.
- Open a text editor and create a new file named
getimage.sh. - Add the following code to the file:
#!/bin/bash
## Extract image URL
image_urls=$(grep -o "\!\[.*]\(.*\)" "$1" | sed -E "s/(\!\[.*]\()(.+)(.*\))/\2/g")
## Print image URL
echo "$image_urls"
This script uses the grep command to find all lines in the Markdown file that contain image links, and then uses the sed command to extract the URL from each line.
Run the Script
Now that you have created the getimage.sh script, you can run it to extract the image URLs from a Markdown file.
- Open a terminal and navigate to the directory where you saved the
getimage.shscript. - Run the script with the path to the Markdown file as an argument:
./getimage.sh /home/labex/project/labex_lab1.md
This will output all the image URLs found in the labex_lab1.md file, one per line.
For example, the output might look like this:
https://doc.shiyanlou.com/document-uid13labid292timestamp14677222211211.png
https://doc.shiyanlou.com/document-uid13labid292timestamp14672311234511.png
https://doc.shiyanlou.com/document-uid13labid292timestamp14677029556772.png
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



