2

I have been learning about shell scripting and regular expressions. I want to find a way to extract a specific part from the file name below.

 profiles_060315091024_30398-r-00006.avro 

I want to extract the number 30398 from the file name.

Thanks

2 Answers 2

1

using awk:

str='profiles_060315091024_30398-r-00006.avro'
awk -F'[_-]' '{print $3}' <<< "$str"
30398

-F'[_-]' is setting a custom field separator as _ or -

Sign up to request clarification or add additional context in comments.

5 Comments

Would I have to change something if I wanted to store the number "30398" in a variable? Sorry I should've asked that in the initial question.
You can do: myvar=$(awk -F'[_-]' '{print $3}' <<< "$str")
Thank very much. Is the '{print $3}' necessary? What is it used for?
yes print $3 is required so that awk can print 3rd field in the given input.
Could also be done without invoking awk as IFS='_-' read x x myvar x <<< "${str}"...
0

Using shell only. I assume you want the digits between the underscore and the hyphen.

  1. regular expression (bash specific)

    filename=profiles_060315091024_30398-r-00006.avro
    if [[ $filename =~ _([0-9]+)- ]]; then num=${BASH_REMATCH[1]}; fi
    echo $num
    # => 30398
    
  2. using parameter expansion (any POSIX shell)

    tmp=${filename##*_}  # remove from the start up to last underscore
    tmp=${tmp%%-*}       # remove the first hyphen until end of string
    echo $tmp
    # => 30398
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.