-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
  • 84,225
  • 14
  • 89
  • 153

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
  • 668
  • 1
  • 5
  • 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
  • 13,382
  • 1
  • 17
  • 36
-1

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

Gustavo Kawamoto
  • 2,665
  • 18
  • 27