Unix shell脚本,需要将文本文件值分配给sed命令

时间:2015-11-02 12:41:03

标签: shell unix sed

我试图将文本文件中的行添加到sed命令 的 observered_list.txt

Uncaught SlingException
cannot render resource
IncludeTag Error
Recursive invocation
Reference component error

我需要将它编码为以下

sed '/Uncaught SlingException\|cannot render resource\|IncludeTag Error\|Recursive invocation\|Reference component error/ d'

帮助我做到这一点。

2 个答案:

答案 0 :(得分:0)

我建议您创建一个sed脚本并连续删除每个模式:

while read -r pattern; do
     printf "/%s/ d;\n" "$pattern"
done < observered_list.txt >> remove_patterns.sed

# now invoke sed on the file you want to modify
sed -f remove_patterns.sed file_to_clean

或者你可以像这样构造sed命令:

pattern=
while read -r line; do
   pattern=$pattern'\|'$line
done < observered_list.txt
# strip of first and last \|
pattern=${pattern#\\\|}
pattern=${pattern%\\\|}
printf "sed '/%s/ d'\n" "$pattern"
# you still need to invoke the command, it's just printed

答案 1 :(得分:0)

您可以使用grep

grep -vFf /file/with/patterns.txt /file/to/process.txt

说明:

-v excludes lines of process.txt which match one of the patterns from output
-F treats patterns in patterns.txt as fixed strings instead of regexes (looks like this is desired here)
-f reads patterns from patterns.txt

查看man grep以获取更多信息。