I'm trying to make a script that uses awk to get the recent file names from .config/okularrc and pipe them into dmenu, and back to awk to get the path for the name selected.
However, I'm getting an error that tells me that there is a character that should be escaped in the regex.
Okularrc file:
[Recent Files]
File1[$e]=$HOME/documents/med/oh/OH Lecture (12)_Lead Poisoning Summary.pdf
File2[$e]=$HOME/ug/Biochemistry/1. biochemistry of kidney.pdf
File3[$e]=$HOME/ug/Archives/3. UG-(mid+final)-WAREED.pdf
File4[$e]=$HOME/ug/Biochemistry/1. b)iochemistry of kidney.pdf
Name1[$e]=OH Lecture (12)_Lead Poisoning Summary.pdf
Name2[$e]=1. biochemistry of kidney.pdf
Name3[$e]=3. UG-(mid+final)-WAREED.pdf
Name4[$e]=1. b)iochemistry of kidney.pdf
the script I made:
#!/bin/bash
DEBUG=1
# Get the names of the files
names=$(awk -F'=' '/^Name/{print $2} ' ~/.config/okularrc)
[ $DEBUG == 1 ] && echo -e "[*] Names are:\n$names\n"
# Pass the names to dmenu and select a file
selected_name=$(echo "$names" | dmenu -l 10 -i -p "Select a recent file:")
[ $DEBUG == 1 ] && echo -e "[*] Selected name is:\n$selected_name\n"
# Get the path of the selected file
file_path=$(awk -v name="$selected_name" -F'=' '$2 ~ name "$" && /^File/ {print $2}' ~/.config/okularrc)
[ $DEBUG == 1 ] && echo -e "[*] File path is:\n${file_path}\n"
# Run Okular with the path of the file
okular "${file_path/'$HOME'/$HOME}"
# If history is zero just start okular; TODO add browser
#[ -z $names ] && okular && exit
and when i run this on files that have "(" it gives me this output:
[*] Names are:
1. b(iochemistry of kidney.pdf
[*] Selected name is:
1. b(iochemistry of kidney.pdf
awk: cmd. line:1: (FILENAME=/home/anonymous/.config/okularrc FNR=1) fatal: invalid regexp: Unmatched ( or \(: /1. b(iochemistry of kidney.pdf$/
[*] File path is:
I'm new to awk and i want to learn more, your help would be much appreciated
~
/
with\/
, but that in itself would be hard to escape correctly (I would guess\\/
). But for a generic regex that may contain any of()[]{}?*+^$|
, the only viable option might be to escape every character by prefixing with `\` (I think spurious escapes are ignored).