Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Edit: added timing comparisons (at end) and got rid of the <code>tr</code></p> <p>It is possible to <em>replace</em> the content of array elements using bash <em><a href="http://wiki.bash-hackers.org/syntax/pe" rel="nofollow">Parameter expansion</a></em>, ie. <code>${var[@]....}</code> but you cannot actually <em>delete</em> the elements. The best you can get with parameter expansion is to make null ("") those elements you do not want. </p> <p>Instead, you can use <code>printf</code> and <code>sed</code>, and <code>IFS</code>. This has the advantabe of allowing you to use full regular expression syntax (not just the shell globbing expressions)... and it is <em>much</em> faster than using a loop... </p> <p>This example leaves array elements which contain <code>c</code><br> <strong>Note:</strong> This method caters for spaces in data. This is achieved via <code>IFS=\n</code> </p> <pre><code>IFS=$'\n'; a=($(printf '%s\n' "${a[@]}" |sed '/c/!d')) </code></pre> <p>Here it is again with before/after dumps: </p> <pre><code>#!/bin/bash a=(one.ac two.b tree.c four.b "five b abcdefg" ) echo "======== Original array ====" printf '%s\n' "${a[@]}" echo "======== Array containing only the matched elements 'c' ====" IFS=$'\n'; a=($(printf '%s\n' "${a[@]}" |sed '/c/!d')) printf '%s\n' "${a[@]}" echo "========" </code></pre> <p>Output: </p> <pre><code>======== Original array ==== one.ac two.b tree.c four.b five b abcdefg ======== Array containing only the matched elements 'c' ==== one.ac tree.c five b abcdefg ======== </code></pre> <hr> <p>For general reference: Testing an array containing 10k elements. selecting 5k: </p> <pre><code>a=( a\ \ \ \ b{0..9999} ) </code></pre> <p> The printf method took: <code>0m0.226s</code> (results in sequential index values)<br>     The first loop method: <code>0m4.007s</code> (it leaves gaps in index values)<br> The second loop method: <code>0m7.862s</code> (results in sequential index values) </p> <p>printf method: </p> <pre><code>IFS=$'\n'; a=($(printf '%s\n' "${a[@]}" |sed '/.*[5-9]...$/!d')) </code></pre> <p>1st loop method: </p> <pre><code>iz=${#a[@]}; j=0 for ((i=0; i&lt;iz; i++)) ;do [[ ! "${a[i]}" =~ .*[5-9]...$ ]] &amp;&amp; unset a[$i] done </code></pre> <p>2nd loop method: </p> <pre><code>iz=${#a[@]}; j=0 for ((i=0; i&lt;iz; i++)) ;do if [[ ! "${a[i]}" =~ .*[5-9]...$ ]] ;then unset a[$i] else a[$j]="${a[i]}=$i=$j"; ((j!=i)) &amp;&amp; unset a[$i]; ((j+=1)); fi done </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload