bash: how to delete elements from an array based on a pattern

前端 未结 6 1131
后悔当初
后悔当初 2020-12-03 07:26

Say I have a bash array (e.g. the array of all parameters) and want to delete all parameters matching a certain pattern or alternatively copy all remaining elements to a new

6条回答
  •  情深已故
    2020-12-03 08:06

    Here's a way using grep:

    (IFS=$'\n' && echo "${MY_ARR[*]}") | grep '[^.]*.pattern/[^.]*.txt'
    

    The meat here is that IFS=$'\n' causes "${MY_ARR[*]}" to expand with newlines separating the items, so it can be piped through grep.

    In particular, this will handle spaces embedded inside the items of the array.

提交回复
热议问题