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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:34:01+00:00 2026-06-11T04:34:01+00:00

For example echo abc-1234a : | grep <do-something> to print only abc-1234a

  • 0

For example

echo "abc-1234a :" | grep <do-something>

to print only abc-1234a

  • 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-06-11T04:34:03+00:00Added an answer on June 11, 2026 at 4:34 am

    I think these are closer to what you’re getting at, but without knowing what you’re really trying to achieve, it’s hard to say.

    echo "abc-1234a :" | egrep -o '^[^:]+'
    

    … though this will also match lines that have no colon. If you only want lines with colons, and you must use only grep, this might work:

    echo "abc-1234a :" | grep : | egrep -o '^[^:]+'
    

    Of course, this only makes sense if your echo "abc-1234a :" is an example that would be replace with possibly multiple lines of input.

    The smallest tool you could use is probably cut:

    echo "abc-1234a :" | cut -d: -f1
    

    And sed is always available…

    echo "abc-1234a :" | sed 's/ *:.*//'
    

    For this last one, if you only want to print lines that include a colon, change it to:

    echo "abc-1234a :" | sed -ne 's/ *:.*//p'
    

    Heck, you could even do this in pure bash:

    while read line; do
      field="${line%%:*}"
      # do stuff with $field
    done <<<"abc-1234a :"
    

    For information on the %% bit, you can man bash and search for “Parameter Expansion“.

    UPDATE:

    You said:

    It’s the characters in the first line of input before the colon. The
    input could have multiple line though.

    The solutions with grep probably aren’t your best choice, then, since they’ll also print data from subsequent lines that might include colons. Of course, there are many ways to solve this requirement as well. We’ll start with sample input:

    $ function sample { printf "abc-1234a:foo\nbar baz:\nNarf\n"; }
    $ sample
    abc-1234a:foo
    bar baz:
    Narf
    

    You could use multiple pipes, for example:

    $ sample | head -1 | grep -Eo '^[^:]*'
    abc-1234a
    $ sample | head -1 | cut -d: -f1      
    abc-1234a
    

    Or you could use sed to process only the first line:

    $ sample | sed -ne '1s/:.*//p'
    abc-1234a
    

    Or tell sed to exit after printing the first line (which is faster than reading the whole file):

    $ sample | sed 's/:.*//;q'
    abc-1234a
    

    Or do the same thing but only show output if a colon was found (for safety):

    $ sample | sed -ne 's/:.*//p;q'
    abc-1234a
    

    Or have awk do the same thing (as the last 3 examples, respectively):

    $ sample | awk '{sub(/:.*/,"")} NR==1'
    abc-1234a
    $ sample | awk 'NR>1{nextfile} {sub(/:.*/,"")} 1'
    abc-1234a
    $ sample | awk 'NR>1{nextfile} sub(/:.*/,"")'
    abc-1234a
    

    Or in bash, with no pipes at all:

    $ read line < <(sample)
    $ printf '%s\n' "${line%%:*}"
    abc-1234a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

grep fails when using both --ignore-case and --only-match options. Example: $ echo abc |
How can I print a $_POST ? Example: echo $_POST['data']; This returns nothing...
How to run executable in Xcode with piped input? For example: echo abc |
I have a command, for example: echo "word1 word2" . I want to put
I'm looking for a elaborate list comparing different operations in PHP. For example: echo
Perhaps easiest to explain with an example: $ echo '\&|' \&| $ echo '\&|'
Consider this simple example (which displays in red): echo -e \033[31mHello World\033[0m It displays
Let's say for example I wanted to echo You are using Windows! or You
For example… This PHP code <?php echo '<html>'; echo '<body>'; echo '<h1>Header One</h1>'; echo
For example, in foo.php: <?php echo 'hello'; ?> And in bar.php, I want to

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.