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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:02:26+00:00 2026-05-31T13:02:26+00:00

I have a file, which holds phone number data, and also some useless stuff.

  • 0

I have a file, which holds phone number data, and also some useless stuff.
I’m trying to parse the numbers out, and when there is only 1 phone number / line, it’s not problem.
But when I have multiple numbers, sed matches the last one (even though everywhere it says it should match only match the first pattern?), and I can’t get other numbers out..

My data.txt:

bla bla bla NUM:09011111111 bla bla bla bla NUM:08022222222 bla bla bla

When I parse for the data, my idea was first to remove all the “initial” “bla bla bla” in front of the first phone number (so I search for first occurrence of ‘NUM:’), then I remove all the stuff after phone number, and get the number.
After that I want to parse the next occurrence from the leftover string.

So now when I try to sed it, I always get the last number on the line:

>sed 's/.*NUM://' data.txt
08022222222 bla bla bla
> 

Primarily I would like to understand what’s wrong with my understanding of SED. Of course more efficient suggestions are welcome!
Doesn’t my sed command say, replace all stuff before ‘NUM:’ with ” (empty)? Why it matches always the last occurrence ?

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-31T13:02:27+00:00Added an answer on May 31, 2026 at 1:02 pm

    This might work for you:

    echo "bla bla bla NUM:09011111111 bla bla bla bla NUM:08022222222 bla bla bla" |
    sed 's/NUM:/\n&/g;s/[^\n]*\n\(NUM:[0-9]*\)[^\n]*/\1 /g;s/.$//'
    NUM:09011111111 NUM:08022222222
    

    The problem you have is understanding that the .* is greedy i.e. it matches the longest match not the first match. By placing a unique character (\n sed uses it as a line delimiter so it cannot exist in the line) in front of the string we’re interested in (NUM:...) and deleting everything that is not that unique character [^\n]* followed by the unique character \n, we effectively split the string into manageable pieces.

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

Sidebar

Related Questions

I have a myism table 'test' which holds some out-dated data, now I want
I have a file which is an XML representation of some data that is
I have a .rc file which is used to include some text data in
I have a XML file which holds configuration data for data sources, and the
I have .zip file which contain csv data. I am reading .zip file using
I have a txt file which actually is a html source of some webpage.
I have a config.php file for one of my site which holds information needed
I have a selectedArray defined as a property in my file that holds which
I have an array which holds a record from a CSV file. Can I
I have a vector of strings: std::vector<std::string> fName which holds a list of file

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.