awk '/user/ {print $1 }' /etc/userdomains | sed 's/://'
the format of /etc/userdomains is
domain.tld: user
otherdomain.tld: otheruser
awk '/user/ {print $1 }' /etc/userdomains | sed 's/://'
the format of /etc/userdomains is
domain.tld: user
otherdomain.tld: otheruser
The easiest way is to set the field separator to ":"
awk -F":" '$2~/user/{print $1}' /etc/userdomains
And if you want to check for exact username,
awk -F"[ \t]*:[ \t]*" '$2=="user"{print $1}' /etc/userdomains
You can use gsub
in awk
to remove all :
s in the string.
awk '/user/ {gsub(":", ""); print $1}' /etc/userdomains
awk
has a sub(regexp, replacement, target)
function that finds the first occurrence of regexp
in target
($0
by default) and replaces it with replacement
(in-place):
awk '/user/ {sub(/:/, ""); print $1}' /etc/userdomains
You could use tr
instead of sed:
awk '/user/ {print $1 }' /etc/userdomains | tr -d ":"
Though I don't see how that's better than just using awk (nor do I see what's wrong with sed).
tr
, since i believe OP wants to get rid of extra commands. Just one awk command does the job.
– user1606
Oct 24 '10 at 04:23
/user/
is like$0 ~ /user/
–$2 ~ /user/
means "does$2
match/user/
"? Also, if the stringuser
can vary depending on input, be sure to use-v
to avoid having to deal with quoting issues, e.g.awk -v "user=$USER" '$2==user{print$1}'
. – ephemient Oct 24 '10 at 20:11