Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7675337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:56:20+00:00 2026-05-31T16:56:20+00:00

I will be short, what i have is array=( one.a two.b tree.c four.b five_b_abc)

  • 0

I will be short, what i have is

array=( one.a two.b tree.c four.b five_b_abc)

I want this

array=( two.b four.b five_b_abc )

from here i found this

# replace any array item matching "b*" with "foo"
array=( foo bar baz )
array=( "${array[@]/%b*/foo}" )
echo "${orig[@]}"$'\n'"${array[@]}"

how ever this does not work

array2=( ${array[@]//%^.p/})

result array2=array

this deletes all with p

array2=(${array[@]/*p*/})

result array2=( one.a tree.c )

I need an idea how to do add ^p (all accept p), and get my solution

array2=(${array[@]/*^p*/}

Its a rather large array about 10k elements where i need to do this i need it to be as fast as possible with data so please no loop solutions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:56:21+00:00Added an answer on May 31, 2026 at 4:56 pm

    Edit: added timing comparisons (at end) and got rid of the tr

    It is possible to replace the content of array elements using bash Parameter expansion, ie. ${var[@]....} but you cannot actually delete the elements. The best you can get with parameter expansion is to make null (“”) those elements you do not want.

    Instead, you can use printf and sed, and IFS. This has the advantabe of allowing you to use full regular expression syntax (not just the shell globbing expressions)… and it is much faster than using a loop…

    This example leaves array elements which contain c
    Note: This method caters for spaces in data. This is achieved via IFS=\n

    IFS=$'\n'; a=($(printf '%s\n' "${a[@]}" |sed '/c/!d'))
    

    Here it is again with before/after dumps:

    #!/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 "========"
    

    Output:

    ======== 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
    ========
    

    For general reference: Testing an array containing 10k elements. selecting 5k:

    a=( a\ \ \ \ b{0..9999} )
    

     The printf method took: 0m0.226s (results in sequential index values)
        The first loop method: 0m4.007s (it leaves gaps in index values)
    The second loop method: 0m7.862s (results in sequential index values)

    printf method:

    IFS=$'\n'; a=($(printf '%s\n' "${a[@]}" |sed '/.*[5-9]...$/!d'))
    

    1st loop method:

    iz=${#a[@]}; j=0
    for ((i=0; i<iz; i++)) ;do
        [[ ! "${a[i]}" =~ .*[5-9]...$ ]] && unset a[$i]
    done
    

    2nd loop method:

    iz=${#a[@]}; j=0
    for ((i=0; i<iz; i++)) ;do
        if [[ ! "${a[i]}" =~ .*[5-9]...$ ]] ;then
            unset a[$i]
        else 
            a[$j]="${a[i]}=$i=$j"; ((j!=i)) && unset a[$i]; ((j+=1)); 
        fi
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a simple multidimensional structure, like this one: somestr<-array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6))
I have an array like this one: $a = array(MA1 => 0, MA10 =>
array one: 1,3,5,7 array two: 2,4,6,8 the array i want would be 1,2,3,4,5,6,7,8 I'm
I have two pair of arrays. In each pair one array contains the index
I have a small application with two arrays. One array is the names, one
This question will be short and sweet. I know an instruction can occur between
i have seen a few days ago such problem there is given two array
I have two threads and one class. Thread1 updates the local object of the
One of my friend was asked this question in an interview - You have
I have two arrays, one of the arrays holds all the unique values of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.