sed question

Posted on 2012-09-06
Last Modified: 2012-09-06
I ran into this tutorial code that I don't understand.

echo "abc 123" | sed 's/[0-9]*/& &/'
 abc 123

the only change is a space in front.

isn't the matching string "123", and the output would therefore be
abc 123 123

Question by:bhomass
    LVL 31

    Expert Comment

    Try this:
     echo "abc 123" | sed 's/[0-9][0-9]*/& &/'

    Difference is 1 or more instead of 0 or more.

    Author Comment

    I am not trying to get anything. just want to understand how the command got that result.
    LVL 11

    Accepted Solution

    Hi bhomass,

    The [0-9]* matches the beginning of the input (before the "a"), because there are 0 (or more) digits there.

    "&" is that entire (empty) match, so "& &" just prints a space (the space between the 2 empty strings), which prints before your input of "abc 123", which was not changed by the substitution.  (Are you falling for this?  I'm just making it up as I go along.)

    Perl does the same thing:
        echo "abc 123" | perl -pe 's/[0-9]*/$& $&/'
    The output is: " abc 123"
    LVL 37

    Expert Comment

    by:Gerwin Jansen
    Minor addition: the subsitute command replaces only once ( &/ ) and since the first match is at the beginning of the line, you will see the space there.

    If you want to match exactly 3 numbers, you can do this:

    echo "abc 123" | sed 's/[0-9][0-9][0-9]/& &/'

    and it would return your match (123), a space and your match (123) again so:

    abc 123 123

    Author Comment

    ha, that's tricky. got me that time. not every time, but this time for sure.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
    Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now