【发布时间】:2016-12-01 20:11:43
【问题描述】:
我正在尝试列出姓名:
list = a, b, c, d, e
sed "/$list/d" output_file.txt
这样包含这些变量 a、b、c、d、e 的行将从 output_file 中排除
请大家帮忙
【问题讨论】:
标签: linux bash shell command-line sed
我正在尝试列出姓名:
list = a, b, c, d, e
sed "/$list/d" output_file.txt
这样包含这些变量 a、b、c、d、e 的行将从 output_file 中排除
请大家帮忙
【问题讨论】:
标签: linux bash shell command-line sed
使用 GNU grep:
list=(a b c d e)
(IFS="|"; grep -vE "(${list[*]})" file)
或使用 GNU sed:
list=(a b c d e)
(IFS="|"; sed -E "/(${list[*]})/d" file)
【讨论】:
list=(a b c d e); (IFS="|"; grep -vE "(${list[*]})" input_file > output_file)
您可以使用 BASH 数组生成带有管道的正则表达式(交替):
list=(a b c d e) # original array
printf -v re "%s|" "${list[@]}" # genera a regex delimited by |
sed -E "/${re%|}/d" file # use sed on this regex
【讨论】:
sed 保存更改,请使用:sed -E -i.bak "/${re%|}/d" file