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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:53:17+00:00 2026-05-14T14:53:17+00:00

I want to extract a substring matching a pattern and save it to a

  • 0

I want to extract a substring matching a pattern and save it to a file. An example string:

Apr 12 19:24:17 PC_NMG kernel: sd 11:0:0:0: [sdf] Attached SCSI removable disk

I want to extract the part between the brackets, in this case [sdf].

I tried to do something like grep -e '[$subtext]' to save the text in the brackets to a variable. Of course it doesn’t work, but I am looking for a way similar to this. It would be very elegant to include a variable in a regex like this. What can I do best?

Thanks!

  • 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-14T14:53:18+00:00Added an answer on May 14, 2026 at 2:53 pm

    There’s probably a better way using bash only, but:

    echo 'Apr 12 19:24:17 PC_NMG kernel: sd 11:0:0:0: [sdf] Attached SCSI removable disk' \
    | sed -s 's/.*\[\(.*\)\].*/\1/'
    

    As Jurgen points out, this matches non-matching lines. If you don’t want to output nonmatching lines, use ‘-n’ so it doesn’t output the pattern, and ‘/p’ to outputs the pattern when it matches.

    | sed -n 's/.*\[\(.*\)\].*/\1/p'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to extract the value between the apostrophes, for example from this string:
I'm trying to extract a substring in this format: preceeding-string-data-i-want Where: preceeding-string should be
I have a string $url = http://www.example.com/index.php?1234567 I only want the substring http://www.example.com, that
I want to extract a substring from a string. I have strings like this:
How can I extract a substring from within a string in Ruby? Example: String1
I want to extract a substring from a line in Perl. Let me explain
I want to extract word after first 'from' in the following string- anti-CD24 from
I want to extract the file name 13572_BranchInformationReport_2012-06-28.zip from the following text - 1:30,/icons/def13572_BranchInformationReport_2012-06-28.zip,13572_BranchInformationReport_2012-06-28.zip,0,184296,Jun
I want to extract the base name from a string, like /opt/home/etc/sample That is,
I want to extract an ID from a string which has the form somethinguseless_XXXXX

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.