awk의 변수 안의 문자를 이스케이프 처리하는 방법은 무엇입니까?

awk의 변수 안의 문자를 이스케이프 처리하는 방법은 무엇입니까?

awk를 사용하여 .config/okularrc에서 가장 가까운 파일 이름을 가져와 dmenu로 파이프한 다음 awk를 반환하여 선택한 이름의 경로를 가져오는 스크립트를 만들려고 합니다. 그러나 정규식에서 이스케이프해야 하는 문자가 있다는 오류가 발생합니다.
Okularrc 파일:

[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

내가 만든 스크립트:

#!/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 

"("가 있는 파일에 대해 이 명령을 실행하면 다음과 같은 출력이 표시됩니다.

[*] 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:

저는 awk를 처음 접했고 더 배우고 싶습니다. 도움을 주시면 대단히 감사하겠습니다.

답변1

다음 줄을 대체했습니다.

file_path=$(awk -v name="$selected_name" -F'=' '$2 ~ name "$" && /^File/ {print $2}' ~/.config/okularrc)

다음 줄을 사용하세요.

file_path=$(awk -v name="$selected_name" -F'=' 'index($2,name) && /^File/ {print $2}' ~/.config/okularrc)

크레딧 @steeldriver

관련 정보