Operating System - HP-UX
1833408 Members
3066 Online
110052 Solutions
New Discussion

Re: help with grep ' :${a[n]}' question

 
SOLVED
Go to solution
Eric Rainer
Occasional Advisor

help with grep ' :${a[n]}' question

Hello
I am creating a script (ksh) to match specific users with their displays.

Originally I ran

cat /data/cit_rugby|grep :${a[n]}

but then I found I had the following problem as time also has the colon format.

---

cat /data/cit_rugby|grep :15
user1 6203 6202 0 09:37:15 ? 0:00 /opt/CTXSmf/slib/ctxlogin -display :14
user2 28668 28667 0 17:21:03 ? 0:00 /opt/CTXSmf/slib/ctxlogin -display :15

reading the man pages on grep I tried using quotes ' ' but I was unable to use a variable. Typing the variable in returns the correct answer.

cat /data/cit_rugby|grep ' :15'
user2 28668 28667 0 17:21:03 ? 0:00 /opt/CTXSmf/slib/ctxlogin -display :15

How do I use a variable within the quotes or is there another way to do this.

thanks

Eric
5 REPLIES 5
curt larson_1
Honored Contributor
Solution

Re: help with grep ' :${a[n]}' question

you'll need to use double quotes (") instead of the single qoute (')

to remove the normal meaning that ksh places on | or on any other special character, you must quote it. you can quote a single character by preceding it with a backslash (\).

Literal (single) quotes, '..' removes the special meaning of all characters except '.

Grouping (double) quotes, "...", removes the special meaning of all characters except $,\,and `. if the $ is not quoted, then varialbe substitution is doen inside double quotes.

curt larson_1
Honored Contributor

Re: help with grep ' :${a[n]}' question

instead of grep try using sed

sed '/.*ctxlogin.*:'${a[n]}'/p'
curt larson_1
Honored Contributor

Re: help with grep ' :${a[n]}' question

or use awk

awk -F: num=${a[n]} '{ if ( $NF == num ) print $0;}'
harry d brown jr
Honored Contributor

Re: help with grep ' :${a[n]}' question

Eric,

Are you trying to match users to their processes or what users are using what display devices (which usually a "who -u" will suffice).

live free or die
harry
Live Free or Die
Eric Rainer
Occasional Advisor

Re: help with grep ' :${a[n]}' question

Thanks for the help

Eric