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 7529355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:38:24+00:00 2026-05-30T04:38:24+00:00

I have a shell variable: all_apk_file=a 1 2.apk x.apk y m.apk I want to

  • 0

I have a shell variable:

all_apk_file="a 1 2.apk x.apk y m.apk"

I want to replace the a 1 2.apk with TEST, using the command:

echo $all_apk_file | sed 's/(.*apk ){1}/TEST/g'

The .*apk means end with apk, {1} means only match one time, but it doesn’t work; I only got the original variable as output: a 1 2.apk x.apk y m.apk

Can anyone tell me why?

  • 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-30T04:38:26+00:00Added an answer on May 30, 2026 at 4:38 am

    One part of the problem is that in regular sed, the () and {} are ordinary characters in patterns until escaped with backslashes. Since there are no parentheses in the variable’s value, the regex never matches. With GNU sed, you can also enable extended regular expressions with the -r flag. If you fix that problem, you will then run into the problem that .* is greedy, and the g modifier actually doesn’t change anything:

    $ echo $all_apk_file | sed 's/\(.*apk \)\{1\}/TEST/g'
    TESTy m.apk
    $ echo $all_apk_file | sed -r 's/(.*apk ){1}/TEST/g'
    TESTy m.apk
    $ echo $all_apk_file | sed -r 's/(.*apk ){1}/TEST/'
    TESTy m.apk
    $
    

    It only stops there because there isn’t a space after m.apk in the echoed value of the variable.

    The issue now is: what is it that you want replaced? It sounds like ‘everything up to and including the first occurrence of apk at the end of a word. This is probably most easily done with trailing context or non-greedy matching as found in Perl regular expressions. If switching to Perl is an option, do so. If not, it is not trivial in normal sed regular expressions.

    $ echo $all_apk_file | sed 's/^[^.]* [^.][^.]*\.apk /TEST /'
    TEST x.apk y m.apk
    $
    

    This looks for anything without dots in it, followed by a blank, followed by no dots again, and .apk; this means that the first dot allowed is the one in 2.apk. It works for the sample data; it would not work if the variable contained:

    all_apk_file="a 1.2 2.apk m.apk y.apk 37"
    

    You’ll need to tune this to meet your requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a variable in a shell script, var=1234_number I want to replace all
I have a shell script with a lot of echo statements. I want to
I have a shell variable which points to the directory where all my configuration
I have a shell script that saves the output of a command that is
Is there a single, universal bash shell variable or common Linux command that will
I am only using apache-ant and not ant-contrib I have an ant target <target
I have a problem where I want to create a variable that looks like
I have a Java application, using a jvm variable. Normally, I set it using
Have a shell script that reads the files in a particular directory. #!/bin/bash for
I have a shell script in which I have to execute two queries (on

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.