[英]how to use array in sed shell command?
我正在尝试列出姓名:
list = a, b, c, d, e
sed "/$list/d" output_file.txt
因此包含这些变量a,b,c,d,e的行将从output_file中排除
有人请帮忙
使用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)
您可以使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.