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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:46:53+00:00 2026-05-27T18:46:53+00:00

I have a regular expression that has several matches in a textfile. I want

  • 0

I have a regular expression that has several matches in a textfile.
I want to copy only the the matches to a second file. I dont want to copy the lines that contain the matches: I only want the matched text.

I dont find a way to do this in notepad++ (only copies complete lines, not only the matches). Also not in Visual Studio search.

Is there a way to copy only matches? Maybe in grepp or sed?

  • 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-27T18:46:54+00:00Added an answer on May 27, 2026 at 6:46 pm

    You can do it with both. Lets say I have a following file –

    Sample file:

    [jaypal:~/Temp] cat myfile 
    this is some random number 424-555
    and my cell is 111-222-3333
    and 42555 is my zip code
    

    And I want to capture only numbers from myfile

    Using sed:

    With sed you can use the combination of -n and p option along with grouped pattern.

    sed -n 's/.[^0-9]*\([0-9-]\+\).*/\1/p'
       |   |          |          |  |  ||
        ---            ----------    -- |
         |                  |        |  ---------> `p` prints only matched output. Since
         V                  V        V              we are suppressing everything with -n
     Suppress       Escaped `(`      \1 prints      we use p to invoke printing.
     output        start the group   first matched   
                   you can reference  group
                   it with \1. If you
                   have more grouped
                   pattern then they can
                   be called with \2 ...
    

    Test:

    [jaypal:~/Temp] sed -n 's/.[^0-9]*\([0-9-]\+\).*/\1/p' myfile 
    424-555
    111-222-3333
    42555
    

    You can simply re-direct this to another file.

    Using grep:

    You can use either –

    egrep -o "regex" filename
    

    or

    grep -E -o "regex" filename
    

    From the man page:

    -E, --extended-regexp
        Interpret PATTERN as an extended regular expression (see below).
    
    -o, --only-matching
        Show only the part of a matching line that matches PATTERN.
    

    Test:

    [jaypal:~/Temp] egrep -o "[0-9-]+" myfile
    424-555
    111-222-3333
    42555
    

    You can simply re-direct this to another file.

    Note: Obviously these are simple examples but it conveys the point.

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

Sidebar

Related Questions

I have this Regular Expression that matches the following strings: <!-- 09-02-2009 ---> <!--
I have a Python regular expression that matches a set of filenames. How to
I want to have the regular expression that makes sure the beginning of the
How can I get a regular expression that matches any string that has two
I need a regular expression in MySQL that matches: a sentence that has a
I want to create regular expression for password that has length of atleast 6
I have a Python regular expression that contains a group which can occur zero
I have the following regular expression that works fine in perl: Classification:\s([^\n]+?)(?:\sRange:\s([^\n]+?))*(?:\sStructural Integrity:\s([^\n]+))*\n The
Does anyone have a regular expression handy that will match any legal DNS hostname
Over the years I have slowly developed a regular expression that validates most email

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.