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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:10:16+00:00 2026-05-28T20:10:16+00:00

A co-worker came to me with a problem I thought would be trivial, but

  • 0

A co-worker came to me with a problem I thought would be trivial, but turned out to be challenging. The challenge is this: given an input string, what Unix commands can be used to print out any matching patterns within the string?

Let’s say we have the following input string.

12345 4444 abc 789012 xyz 1234567 def 987654321 qrz 60606

The goal is to print out any 5-digit or 6-digit numbers within the string, but not any 4-digit or 7-digit numbers.

We first thought of using sed like so:

echo "12345 4444 abc 789012 xyz 1234567 def 987654321 qrz 60606" |
  sed 's/.*[^0-9]\{1\}\([0-9]\{5,6\}\).*/\1/g'

This command, however, only prints out the last occurrence of any matching patterns.

We finally came up with using a combination of sed and grep -Eo.

echo "12345 4444 abc 789012 xyz 1234567 def 987654321 qrz 60606" |
  sed 's/^/ /' | sed 's/$/ /' | grep -Eo '[[:space:]]+[0-9]{5,6}[[:space:]]+' |
  sed 's/ $//' | sed 's/^ //'

It works but seem a bit kludgy. Is there a better way?

  • 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-28T20:10:17+00:00Added an answer on May 28, 2026 at 8:10 pm

    How about tr and grep?

    echo "12345 4444 abc 789012 xyz 1234567 def 987654321 qrz 60606" |
      tr ' ' '\n' |
      grep '^[[:digit:]]\{5,6\}$'
    

    Or as dmckee suggests, you can use the -o flag to grep to skip the tr stage (if your version of grep has this flag):

    echo "12345 4444 abc 789012 xyz 1234567 def 987654321 qrz 60606" |
      grep -o '\<[[:digit:]]\{5,6\}\>'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This came up at work today, so I thought I would throw it out
I've been dealing with this before but it seems like the problem came back.
I have a worker method with an optional parameter Work(string input = DefaultInput) {
I'm doing a website UI and I came across this problem in IE7. As
Here's the deal, I had this app working since iPhone OS 3.0 came out,
Co-worker is sure he checked in a file: foo_oustanding.dpr but isn't sure when/where (we
This question came about as a result of some mixed-language programming. I had a
I came across a problem yesterday where my program failed as 32 bit process.
I recently made some changes to my development environment and came across a problem.
(I'm a complete novice programmer) I recently came across a problem with my custom

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.