I have a file that documents the structure of several directories. I am trying to print the text for each directory individually. My input file looks like this:
$ cat file.txt
/bin:
file_1
file_2
file_3
/sbin:
file_a
file_b
file_c
/usr/local/bin:
doc_a
doc_b
doc_c
What I'm trying to do is print a specific section of the file based on user selection:
#!/bin/bash
PS3=$'\nMake a selection '
select dir in $(grep ':' file.txt;) do
case $REPLY in
[0-9]) echo $dir
# Need something here. Maybe a pcregrep regex?
# pcregrep '(<= $dir)*(some_fancy_regex)' file.txt
break;;
esac
done
The user is presented with menu options:
1) /bin:
2) /sbin:
3) /usr/local/bin:
Make a selection
Suppose the user chooses 2. Currently, this just prints the chosen directory on the screen. I would like to display the directory as well as the files it contains.
/sbin:
file_a
file_b
file_c
From what I've read it seems like a pcre regex would work here. I barely understand non-pcre style regex. I'm trying to wrap my brain around positive and negative lookahead & lookbehind but I really don't know what I'm doing yet. If someone could help me figure this out I would appreciate it.
- All directories begin with a
/
and end with:
- File names listed under each directory may contain:
[a-z]
,[A-Z]
,[0-9]
- Literal characters
.
_
-
[
- All directory / file structures end with a blank empty line