LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-12-2010, 07:22 AM   #1
courteous
LQ Newbie
 
Registered: Dec 2010
Posts: 22

Rep: Reputation: 8
Exclamation $$ variable as GREP pattern: "weird" behavior


First, 35th field of "ls -l /proc/" is column with folder names.

So, the following snippet returns $PID (if found):
Code:
ls -l /proc/ | cut -d' ' -f35 | grep ^${PID}$
This doesn't work:
Code:
ls -l | cut -d' ' -f35 | grep ^$$$

Last edited by courteous; 12-12-2010 at 07:24 AM.
 
Old 12-12-2010, 07:55 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Well I am going to start with .. What are you talking about?
Quote:
First, 35th field of "ls -l /proc/" is column with folder names.
The 35th field? This is based on what as a delimiter? Here is a snippet of 'ls -l /proc' for me:
Code:
dr-xr-xr-x  4 root       root                     0 2010-12-12 23:21 tty
-r--r--r--  1 root       root                     0 2010-12-12 23:21 uptime
-r--r--r--  1 root       root                     0 2010-12-12 23:21 version
-r--r--r--  1 root       root                     0 2010-12-12 23:21 version_signature
-r--------  1 root       root                     0 2010-12-12 23:21 vmallocinfo
-r--r--r--  1 root       root                     0 2010-12-12 23:21 vmstat
-r--r--r--  1 root       root                     0 2010-12-12 23:21 zoneinfo
I think you would agree that it will be difficult to find 35 fields in this??
Quote:
So, the following snippet returns $PID (if found):
How would this ever return a PID?? Last time I looked, ls has no idea about PIDS. And why would it need to if it is simply listing objects?
Quote:
This doesn't work:
As far as I can tell, none of what you have shown works.
 
1 members found this post helpful.
Old 12-12-2010, 09:15 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Hmmm... I agree with grail: it's not clear at all what you're trying to achieve and what you're talking about. Anyway, the grep command with $$ works for me, if the PID of the current shell is expected. Eventually you may try:
Code:
... | grep ^${$}$
 
1 members found this post helpful.
Old 12-12-2010, 09:21 AM   #4
courteous
LQ Newbie
 
Registered: Dec 2010
Posts: 22

Original Poster
Rep: Reputation: 8
Thank you both. 35th field is based on space (' ') delimiter: there must be some better way to extract folder's name (the last "column)? (I've tried with default TAB delimiter, but it doesn't work.)
 
Old 12-12-2010, 09:29 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Why you don't use simply ls (without -l) to list the content of /proc? In this way you can avoid extraction.

To by-pass the multiple spaces problem you can use awk which treats multiple spaces as a single delimiter by default. For example:
Code:
ls -l | awk '{print $NF}'
This works if directories or file names don't have spaces in their name (otherwise they are treated as delimiter as well). A workaround in awk is:
Code:
ls -l | awk '{print substr($0,index($0,$9))}'
if the column containing directories names is the 9th (on some systems is the 8th, depending on the timestamp format).
 
  


Reply



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 can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
command grep issue: how to get occurrences of an pattern look like "cool.a_string" coolloo_djack Linux - General 4 03-13-2010 09:27 AM
Using "OR" type matching pattern with grep command riteshanand40 Linux - Newbie 3 05-06-2009 04:48 AM
c gurus: whats the diff? "var[]" and "*var"? kourama Programming 16 04-03-2007 10:28 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:22 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