Grep exact string not working when piped - grep

If I cat text file containing mysqld_safe and mysqld then grep -w works as expected. But when piped with PS output it does not work.
ps -ef | grep -w mysqld
Output's both lines.
/usr/bin/mysqld_safe
/usr/libexec/mysqld
I am expecting only mysqld. I'm aware of exclude option grep -v mysqld_safe.
Version - grep (GNU grep) 2.5.1

If you have pgrep, using pgrep -x mysqld would be better choice than ps + grep
From man pgrep
pgrep, pkill - look up or signal processes based on name and other attributes
-x, --exact
Only match processes whose names (or command line if -f is specified) exactly match the pattern.
-l, --list-name
List the process name as well as the process ID. (pgrep only.)
-c, --count
Suppress normal output; instead print a count of matching processes. When count does not match any‐
thing, e.g. returns zero, the command will return non-zero value.
-n, --newest
Select only the newest (most recently started) of the matching processes.

Related

ps pipe to grep brackets grep keyword.

A lot of times when I use ps and pipe it into grep, I put one of the grep characters in [ ] square brackets. I do this do the actual call to grep is not included in the ps readout. It helps when I am scripting. Like if I redirect the output to a file, and that file is empty, the process I am looking at is not on. I do not know why bracketing one character of the grep works, and was amused to see that using the brackets is actually faster and takes less processing time.
-bash-3.2$ time ps -ef | grep *.[j]il
real 0m0.027s
user 0m0.012s
sys 0m0.017s
-bash-3.2$ time ps -ef | grep *.jil
zksuy7k 11528 18285 0 20:54 pts/7 00:00:00 grep *.jil
real 0m0.040s
user 0m0.015s
sys 0m0.016s
-bash-3.2$

How do I grep for a pattern in which the pattern is a shell-expansion that generates a list?

echo $'one\ntwo\nthree' | grep -F -v $(echo three$'\n'one)
Output should in theory be the string two
I've read that the -F command lets grep interpret each line as a list connected by 'or' qualifier.
Only mistake is some missing double-quotes:
echo $'one\ntwo\nthree' | grep -F -v "$(echo three$'\n'one)"
Also, keep in mind that this will also filter out "threesome", "someone", etc...
(#etan-reisner points out that running set -x before the original and the fixed command can be used to observe the difference the double-quotes make here, and, more generally, is a useful way to debug bash commands.)

pgrep program with hyphen in linux?

how to pgrep a program contains hyphen? Considering case like bellow.
c source file name, program-contains-hypen.c:
#include <stdio.h>
int main(int argc, char *argv[])
{
getchar();
return 0;
}
then keep editing it with vim, and run the compiled program-contains-hypen in another terminal.
at this time, if I use pgrep program-contains-hypen, nothing got; if I use pgrep -f program-contains-hypen, two results got.
how to exactly get the pid of program program-contains-hypen?
ps:
$ pgrep --version
pgrep from procps-ng 3.3.10
update:
It's not the problem of hyphen but the length of command line.
see https://askubuntu.com/questions/361104/pgrep-pattern-length-limit.
thanks to #user5631389
I think you've answered your own question. pgrep -f is the correct way to do this. If you're getting two results, you have two running processes with that name. You can use pgrep -fa to see the whole command line. For example:
Without -f, you will only be able to use a certain number of characters according to https://askubuntu.com/questions/361104/pgrep-pattern-length-limit. For example:
$ pgrep unity-scope-loader
$ pgrep unity-scope-loade
$ pgrep unity-scope-load
$ pgrep unity-scope-loa
9489
$ pgrep -f unity-scope-loader
9489
$ pgrep -fa unity-scope-loader
9489 /usr/bin/unity-scope-loader applications/applications.scope applications/scopes.scope commands.scope
$ awk '$11~/unity-scope-loader/{print $2}' <(ps aux)
9489

Why does this grep -F not work?

I've used grep -F extensively at work (CentOS) to ignore regex pattern in the match. Now here's what I'm trying at home (Ubuntu 14.04):
$ cat file
Here is
the -F
you were looking
for!
~$ grep -F '-F' file
_
The underscore is meant to show a blinking cursor, as if it's waiting for input. Could it be because Ubuntu's grep doesn't follow all POSIX switches (I read that -F was specified by POSIX) or am I making a mistake somewhere?'
===== Update ======
Interestingly, it fails only when there's a newline following -F. If you change the text to, say, -F option, then the line matches. A bug in grep?
Specifying the same argument twice has no effect, so
grep -F '-F' file
is the same as grep -F file, which of course searches for the fixed-string file in its standard input.
The single-quotes are a red-herring. They protect the -F from any expansion by the shell (like glob-expansion, variable expansion, command substitution, etc.), and are removed by the shell before grep sees the -F.
What you need to do is use grep's -e pattern argument:
grep -F -e '-F' file
Given that context, the -F will be interpreted as the pattern. The single-quotes are still redundant. You could single-quote every other arg. I left them in because it helps humans come to the right conclusion at first glance, and it's generally not bad practice to quote stuff that could contain shell meta-characters in a future version of the script, even if it's currently safe.

How to grep -w for 2 words that might or might not occur in the same line?

I would need the combination of the 2 commands, is there a way to just grep once? Because the file may be really big, >1gb
$ grep -w 'word1' infile
$ grep -w 'word2' infile
I don't need them on the same line like grep for 2 words existing on the same line. I just need to avoid redundant iteration of the whole file
use this:
grep -E -w "word1|word2" infile
or
egrep -w "word1|word2" infile
It will match lines matching either word1, word2 or both.
From man grep:
-E, --extended-regexp
Interpret PATTERN as an extended regular expression (ERE, see below).
Test
$ cat file
The fish [ate] the bird.
[This is some] text.
Here is a number [1001] and another [1201].
$ grep -E -w "is|number" file
[This is some] text.
Here is a number [1001] and another [1201].

Resources