嘗試使用SED。
#!/bin/bash
# The input file is a command line argument.
input_file="${1}"
# The map of variables to their values
declare -A value_map=([abc]=ddd [ccc]=10)
# Loop over the keys in our map.
for variable in "${!value_map[@]}" ; do
echo "Replacing ${variable} with ${value_map[${variable}]} in ${input_file}..."
sed -i "s|${variable}|${value_map[${variable}]}|g" "${input_file}"
done
這個簡單的bash腳本將用給定文件中的ddd和ccc替換爲10的abc。下面是它的工作在一個簡單的文件的例子:
$ cat file.txt
so boo aaa abc
duh
abc
ccc
abcccc
hmm
$ ./replace.sh file.txt
Replacing abc with ddd in file.txt...
Replacing ccc with 10 in file.txt...
$ cat file.txt
so boo aaa ddd
duh
ddd
10
ddd10
hmm
如果您的問題涉及知道'&abc'可以被替換成'ddd'先驗的,它比如果你要進行詞法分析和解析更簡單似乎是一個特定領域的語言。你能澄清你的問題到底是什麼以及你到底做了什麼? –