Need Help Creating a Bash Script to Extract Unique Paths From Markdown Files

0
1
Asked By CuriousCoder42 On

Hey everyone! I'm relatively new to bash scripting and could really use some help. I want to create a script that will scan through all the .md files in a given directory, including subdirectories if possible. The goal is to find and extract unique paths mentioned in the files, specifically paths that aren't links or URLs. For instance, if an .md file contains a reference like "cd /example/path/foo/bar," I want the script to return just the string "/example/path/foo/bar" along with the name(s) of the files where it was found. Is this something that can be done? I feel a bit overwhelmed trying to figure it out on my own!

1 Answer

Answered By BashNinja99 On

You can definitely do this! One approach is to use the following command in your terminal:

```bash
grep -R "cd /" --include="*.md" .
```
Just remember that the `.` at the end refers to the current directory, so change that if you want to search somewhere else. You could also mix `find` and `grep` like this:

```bash
find ~ -iname "*.md" -exec grep --color=no -R -I -H "cd /" {} ;
```
This will help you identify where those paths are found!
If you're interested, there are some tweaks you might make to refine the results further, like ignoring comments or adjusting the search pattern to avoid false positives.

LearningLass -

The suggestions above are solid! To enhance your script a bit, consider piping the output to strip "cd /" so you just have the paths. You might also want to adjust your search to avoid commented lines if that’s something you care about.

DataDiveR -

Another alternative is to use this command:

```bash
find ~ -iname "*.md" -print0 | xargs -0 grep -Hn -E '^[^#]*cd /'
```
Using `-print0` with `find` handles filenames with spaces and should improve performance by collecting results for `grep` instead of calling it multiple times.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.