Issue
I run from time to time a linkchecker over my site and the external links 404 will be saved to a logfile.
Now I try to delete the links automated from the markdown files. I use multilingual websites so I start read in the logfile to an array.
IFS=$'\n'
link=( $(awk '{print $7}' $ext) )
for i in "${link[@]}"; do
grep -r $i content/* | sed -e 's/([^()]*)//g'
done
This command deletes the link and title with () but the [Example Text] remains. I search for a way to remove [] so that at the end I only get Example Text.
Now:
[Example Text](http://example.com "Example Title")
Desired result:
Example Text
Solution
Assumptions
- The
i
infor i in "${link[@]}"
will evaluate to be a link like "http://example.com
" each loop - The format of every section in your markdown file we care about will take on the form you described
[Example Text](http://example.com "Example Title")
The code
IFS=$'\n'
link=( $(awk '{print $7}' $ext) )
for i in "${link[@]}"; do
grep -ro "\[.*\].*${i}" content/* | grep -o '\[.*\]' | tr -d '[]'
done
Explanation
grep -ro "\[.*\].*${i}" content/*
:- Recursive search to run on all files in a dir:
grep -r ... content/*
- Recursive search to run on all files in a dir:
- Print only the text that applies to our regex:
grep -o
- Print only the text that applies to our regex:
- Print anything that starts with
[
followed by anything.*
then a]
followed by the value of our loop variable${i}
(The current link):"\[.*\].*${i}"
- Print anything that starts with
- From that output all we want is
"Example Text"
which lives between the brackets, so anything not between brackets needs to gogrep -o '\[.*\]'
- Finally, we want to remove those pesky brackets:
tr -d '[]'
Answered By - Lenna Answer Checked By - Cary Denson (WPSolving Admin)