-1

If you type id user_name you get uid=500(user_name).

How can I use sed to get everything between the = and ( so you get 500?

I have sed -n ā€˜/=/,/(/p’.

Cyrus
  • 77,979
  • 13
  • 71
  • 125

3 Answers3

1

You can group the number 500 - or any integer value, for that matter - and return it with sed 's/^uid=\([0-9]*\).*/\1/'.

Rfroes87
  • 656
  • 1
  • 4
  • 15
0

@Rfroes87 provided a fine solution in a comment - sed 's/^uid=([0-9]*).*/\1/'

But you can do it a lot of ways that don't involve another process.

You might be able to just use something like $UID or $uidalready present - check your env withset`.

You can use read -

IFS='=(' read x myUID x <<< $(id)

Or you could use parameter parsing in steps -

myUID="$(id)"
myUID="${myUID#*=}"
myUID="${myUID%%(*}"
Paul Hodges
  • 10,927
  • 1
  • 16
  • 30
-1

You can do it using the following: sed 's_^uid=\(.*\)(.*$_\1_g'

Gustavo Kawamoto
  • 2,142
  • 12
  • 24