This question already has answers here:
grepping output of ps, exclude the word grep [duplicate]
(1 answer)
Regular expression for a string containing one word but not another
(5 answers)
Closed 3 years ago.
I am running this script docker-maintenance-script.sh. I want to check if the same clone(s) of program(my script) is running on machine. So I am running a ps command inside the script which greps again docker-maintenance-script.sh.
ps -ef | grep -P "(?<!(grep))(docker\-maintenance\-script\.sh)"
output while running the script was:
dxadmin 4497 5231 0 22:32 pts/0 00:00:00 bash -x docker-maintenance-script.sh
which is only infact this current proceess that is greping.
So to the problem -
I want to discard that entry as that is invoker process. I need ps list that only lists other docker-maintenance-script.sh process. not this process.
I need a ps list that discards my current parent pid based on negative look behind usinf pid.
I tried this
ps -ef | grep -P "(?<!(grep))(?<!($$))(docker\-maintenance\-script\.sh)"
($$ returns current pid of process)
in this case 4497. But that is not helping as it 'not match' only when its just right before
(docker-maintenance-script.sh). I want to not match the string whenever
$$ appears anywhere before (docker-maintenance-script.sh). Please Help
I tried too ps -ef | grep -P "(?<!($$.*))(docker\-maintenance\-script\.sh)"
but that returns non-fixed length lookbehind grep error
Related
This question already has answers here:
How to grep for the whole word
(7 answers)
Closed 6 years ago.
I'm trying to parse the firewall log file and only take the lines that don't contain the router's address as source. The router's address is the obvious 192.168.2.1 and the computer's address is 192.168.2.110.
If I write grep -v 192.168.2.1 then I don't get the destination 192.168.2.110, because it starts with 192.168.2.1. If I don't use anything, then I get the lines from the router, that I would like to filter out. I have searched and tried different regexs, but no matter what I do, I either get both addresses or none.
This force PATTERN to match only whole words grep -w.
grep -v -w 192.168.2.1 file
192.168.2.110
Or Enclose your pattern with \<pattern\>
grep -v '\<192.168.2.1\>' file
192.168.2.110
You can try to use \b which matches with word boundaries:
grep -vP '\b192.168.2.1\b'
or better yet
grep -vP '\b192\.168\.2\.1\b'
You need the -P mode for this to work.
This question already has answers here:
Closed 10 years ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Original close reason(s) were not resolved
Possible Duplicate:
extract regexp result from string and write it to a variable
Here is my command :
grep -E '\*[[:space:]]+FIN[[:space:]]+([^)]+?)') myfile
It outputs :
FIN (SUCCESS)
And I would like it outputs only :
SUCCESS
How can I tell grep to do it ?
You can pipe the output of your grep command to the awk command.
grep -E '*[[:space:]]+FIN[[:space:]]+([^)]+?)') myfile | awk '{print $2}'
I am not sure how to do that with grep alone, as it is not really tailored to that exact use case. Since you are on a platform where grep is, use pipes to your advantage when you can have one command solve part of the problem, and another command the other part.
grep is not capable of outputting a single capture group, but you can use sed to do it instead:
sed 's/\*[[:space:]]\+FIN[[:space:]]\+(\([^)]\+\))/\1/g' file
If you use ack then you can use match groups and the --output switch:
ack '\*\s+FIN\((.+?)\)' --output='$1' myfile
I am using Cygwin on Windows. I want to extract all the lines from a file which contain exactly 9 letters in the name.
To do this, I am using:
cat filename.txt | grep -P "[a-z]{9}"
however this is also returning words of different case and lengths greater than 9.
I have even set the environment variable, LC_ALL to C.
I am able to make this work though:
cat filename.txt | grep -P "^[a-z]*[a-z]$"
And this displays only words with lowercase characters.
Please note that I am running the commands in Cygwin and I have observed that there are certain differences between Cygwin and a Linux Distro. The commands do not work the same way.
Try
cat filename.txt | grep -P "^[a-z]{9}$"
^ = beginning of string
$ = end of string
Your regex returns all words containing lower-case alphabets which have a length which is a multiple of 9.
I am not a regular expressions expert, but I thought I understood the basics. I was reading a tutorial that mentioned using this syntax:
$ ps -ewwo pid,args | grep [s]sh
to determine if SSHD is running or not.
I do not understand why the first s is in brackets. I would think that ssh and [s]sh would yield the same results, but I actually get different results.
$ ps -ewwo pid,args | grep [s]sh
1258 /usr/bin/ssh-agent /usr/bin/dbus-launch --exit-with-session gnome-session --session=ubuntu
2988 /usr/sbin/sshd -D
$ ps -ewwo pid,args | grep ssh
1258 /usr/bin/ssh-agent /usr/bin/dbus-launch --exit-with-session gnome-session --session=ubuntu
2988 /usr/sbin/sshd -D
3082 grep --color=auto ssh
So why does it find the 3rd result in the second example?
Thanks!
The regular expressions [a]bc and abc match exactly the same set of strings, but they're being applied to different data, because the command-line arguments to grep appear in the output of the ps command.
Using [a]bc causes the literal string "[a]bc" to appear in the output of ps -- and this isn't matched by the regular expression [a]bc.
The idea is to avoid matching the line for the grep command itself.
The brackets are a character class but it doesn't really make sense to have a character class with one character and no repeat specified.
The reason you get different results is because ssh matches itself the grep arguments in the process list, but it [s]sh does not match itself.
When you pipe ps into grep, you'll often find the running grep process because the term exists in the program name and it's probable it will match.
This question already has answers here:
Why are there so many different regular expression dialects?
(4 answers)
Closed 1 year ago.
I was working on the Linux box A and I run this:
grep '^\S*\s-' access_log
That displayed some lines, as expected.
Then I moved to the machine B and I launched exactly the same command. But this time it didn't work.
I had to launch this in order to get done what I needed:
grep '^[^ ]* -' access_log
Before succeeding, I tried all of these but with no luck:
grep '^\S* -' access_log
grep '^\S*\s-' access_log
grep -e '^\S* -' access_log
grep -E '^\S* ' access_log
It looks like machine B doesn't understand the metacharacters \S and \s.
Both of the boxes were running: grep 2.5.1 and bash 3.2.25
How is that possible?
Cheers,
Dan
Judging from the grep man page. It seems that if you can use things like \s you are using Perl regular expressions. Which are used when the -P option is passed to grep. So it may be that that option is set automatically on machine A and not on machine B. The reason for that may be some alias, or the option is set in GREP_OPTIONS.