LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-22-2010, 03:38 PM   #1
jasonws
LQ Newbie
 
Registered: Oct 2010
Location: Vermont, USA
Posts: 15

Rep: Reputation: 0
Grep for the result of a command within the result of another command


Hey, I'm trying to figure out how to merge these two commands. The first one needs to be plugged into the second where it says <TTY>. I'm sure it's pretty simple, but I haven't yet found a way to do it. Thanks! -Jason

#Finds the current session of the given user <user>
<TTY> = w |grep <user> |grep 0.00s | cut -c 10-15

#Returns all the pids for all but the current session <TTY> of the given user <user>
ps uU <user> |grep pts/ |grep -v <TTY> | cut -c 10-14
 
Old 10-22-2010, 04:00 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Code:
ps uU <user> |grep pts/ |grep -v $(w |grep <user> |grep 0.00s | cut -c 10-15) | cut -c 10-14
 
Old 10-22-2010, 04:11 PM   #3
jasonws
LQ Newbie
 
Registered: Oct 2010
Location: Vermont, USA
Posts: 15

Original Poster
Rep: Reputation: 0
Sorta figured this out, not really. Help?

Hey, I used the for expression to handle this problem, but apparently this command isn't formed correctly and I get > as a result.

$for tty in 'w |grep dsi |grep 0.00s | cut -c 10-15'; do for pid in 'ps uU dsi |grep pts/ |grep -v $tty | cut -c 10-14'; do kill $pid; done
>

To test it, I tried this command, with the following result:

$for tty in 'w |grep dsi |grep 0.00s | cut -c 10-15'; do echo $tty; done
w |grep dsi |grep 0.00s | cut -c 10-15

Not sure what I'm doing wrong. I would think I'd get some pids, since that's what I get when I run the following command:

w |grep dsi |grep 0.00s | cut -c 10-15

Any ideas?

Thanks,
Jason
 
Old 10-23-2010, 01:22 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,021

Rep: Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199
Quote:
input -> for tty in 'w |grep dsi |grep 0.00s | cut -c 10-15'; do echo $tty; done
output -> w |grep dsi |grep 0.00s | cut -c 10-15
So what is this telling you?

It is saying that tty only gets one value, that being the string 'w |grep dsi |grep 0.00s | cut -c 10-15', which it is then echoing.
Seems to have performed its task correctly.

I am guessing your question is why did it not return the values from your commands?
Answer: You have input a string and not a command substitution.

Have a look at MensaWater's solution and your answer lay there on how to correct it.
 
Old 10-25-2010, 08:27 AM   #5
jasonws
LQ Newbie
 
Registered: Oct 2010
Location: Vermont, USA
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks, MensaWater and grail. I should have reloaded the page before posting last Friday... Here's the final command that kills the PIDs:

ps uU <user> |grep pts/ |grep -v $(w |grep <user> |grep 0.00s | cut -c 10-15) | cut -c 10-14 | xargs kill
 
Old 10-25-2010, 09:07 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,021

Rep: Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199
You should probably be a little cautious ... what happens when your username exceeds 10 characters? Your first cut will be in a bit of strife
 
Old 11-18-2010, 02:39 PM   #7
jasonws
LQ Newbie
 
Registered: Oct 2010
Location: Vermont, USA
Posts: 15

Original Poster
Rep: Reputation: 0
In my case the usernames will always be 3 characters. Nothing to worry about. Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass the result of a command to another command (like grep) desb01 Programming 4 06-25-2009 12:09 PM
command result as varible mkiler Programming 1 03-10-2009 10:22 AM
python - getting command result Hewson Programming 1 08-01-2007 06:22 PM
BASH Command Result redhatnoob Programming 2 08-19-2004 06:25 PM
cp command problem..... unexpected result hamster Linux - General 2 04-10-2003 04:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration