-2
sed -e"s@uid=[0-9]*(@@g"-e"s@).*\$@@g"

I tried

s= search

[0-9]* = search only number starting with 0-9

0

2 Answers 2

5

Your sed command uses @ as delimiters for the s/// (substitute, not "search") command without reason, which makes it difficult to read.

Assuming the missing space before the second -e is a typo, and giving the strings a bit more air for readability, we have

sed -e "s@uid=[0-9]*(@@g" -e "s@).*\$@@g"

This is the same as

sed -e "s/uid=[0-9]*(//g" -e "s/).*\$//g"

which, with single-quoted strings, becomes

sed -e 's/uid=[0-9]*(//g' -e 's/).*$//g'

(note \$$), or

sed 's/uid=[0-9]*(//g; s/).*$//g'

So, this is calling sed with two editing expressions:

  1. s/uid=[0-9]*(//g

    This substitutes any substring uid= followed by any number of digits and a ( by nothing (it removes these strings). The trailing g makes it do this for all non-overlapping matches on the line, not just the first, so that

    uid=1(a) uid=2(b)
    

    is transformed into

    a) b)
    
  2. s/).*$//g

    This removes everything from the first ) on the line to the end of the line by replacing it with nothing. Note that g is not needed here as the expression is guaranteed to only match once due to the anchoring to the end of the line with $.

    Our example from above would be transformed from

    a) b)
    

    into just

    a
    

    which leads me to believe that the trailing g in each command was added without much afterthought (the second one is definitely not needed and the fact that the second expression simply deletes all from the first ) makes the first g ineffective, unless you are dealing with data that has unbalanced parentheses). These should likely be removed.

As a further example, the command would change

this is (line 1) of the file
hello world uid=09(a)  uid=213(b)

into

this is (line 1
hello world a

Without knowing more about the context of this command (where you saw it, what it's supposed to do etc.), it's difficult to say much more about it or to suggest improvements to it.

5

I suspect that it's trying to take a line/variable that contains something like this:

uid=500(JohnSmith)

...and reduce it to something like this:

JohnSmith

It's trying to extract a real/user name — from inside the brackets — by deleting everything outside the brackets, and the brackets themselves.

Of course the number could be any number, and the name could be any name.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .