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

  • Home
  • SEARCH
  • 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 515063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:35:22+00:00 2026-05-13T07:35:22+00:00

I’ve got this script in sh (macOS 10.6) to look through an array of

  • 0

I’ve got this script in sh (macOS 10.6) to look through an array of files:

files="*.jpg"
for f in $files
    do
        echo $f | grep -oEi '[0-9]+_([a-z]+)_[0-9a-z]*'
        name=$?
        echo $name
    done

So far $name merely holds 0, 1 or 2, depending on if grep found that the filename matched the matter provided. What I’d like is to capture what’s inside the parens ([a-z]+) and store that to a variable.

I’d like to use grep only, if possible. If not, please no Python or Perl, etc. sed or something like it – I would like to attack this from the *nix purist angle.

  • 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-13T07:35:22+00:00Added an answer on May 13, 2026 at 7:35 am

    If you’re using Bash, you don’t even have to use grep:

    files="*.jpg"
    regex="[0-9]+_([a-z]+)_[0-9a-z]*" # put the regex in a variable because some patterns won't work if included literally
    for f in $files    # unquoted in order to allow the glob to expand
    do
        if [[ $f =~ $regex ]]
        then
            name="${BASH_REMATCH[1]}"
            echo "${name}.jpg"    # concatenate strings
            name="${name}.jpg"    # same thing stored in a variable
        else
            echo "$f doesn't match" >&2 # this could get noisy if there are a lot of non-matching files
        fi
    done
    

    It’s better to put the regex in a variable. Some patterns won’t work if included literally.

    This uses =~ which is Bash’s regex match operator. The results of the match are saved to an array called $BASH_REMATCH. The first capture group is stored in index 1, the second (if any) in index 2, etc. Index zero is the full match.




    side note #1 regarding regex anchors:

    You should be aware that without anchors, this regex (and the one using grep) will match any of the following examples and more, which may not be what you’re looking for:

    123_abc_d4e5
    xyz123_abc_d4e5
    123_abc_d4e5.xyz
    xyz123_abc_d4e5.xyz
    

    To eliminate the second and fourth examples, make your regex like this:

    ^[0-9]+_([a-z]+)_[0-9a-z]*
    

    which says the string must start with one or more digits. The carat represents the beginning of the string. If you add a dollar sign at the end of the regex, like this:

    ^[0-9]+_([a-z]+)_[0-9a-z]*$
    

    then the third example will also be eliminated since the dot is not among the characters in the regex and the dollar sign represents the end of the string. Note that the fourth example fails this match as well.

    side note #2 regarding grep and the \K operator:

    If you have GNU grep (around 2.5 or later, I think, when the \K operator was added):

    name=$(echo "$f" | grep -Po '(?i)[0-9]+_\K[a-z]+(?=_[0-9a-z]*)').jpg
    

    The \K operator (variable-length look-behind) causes the preceding pattern to match, but doesn’t include the match in the result. The fixed-length equivalent is (?<=) – the pattern would be included before the closing parenthesis. You must use \K if quantifiers may match strings of different lengths (e.g. +, *, {2,4}).

    The (?=) operator matches fixed or variable-length patterns and is called "look-ahead". It also does not include the matched string in the result.

    In order to make the match case-insensitive, the (?i) operator is used. It affects the patterns that follow it so its position is significant.

    The regex might need to be adjusted depending on whether there are other characters in the filename. You’ll note that in this case, I show an example of concatenating a string at the same time that the substring is captured.

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

Sidebar

Ask A Question

Stats

  • Questions 397k
  • Answers 397k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer How about SELECT * FROM (SELECT *, ROW_NUMBER() OVER(PARTITION BY… May 15, 2026 at 3:25 am
  • Editorial Team
    Editorial Team added an answer Must you use a for loop? Copy the original and… May 15, 2026 at 3:25 am
  • Editorial Team
    Editorial Team added an answer I am not sure that I understand why you use… May 15, 2026 at 3:25 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.