Say there are 3 process with names abc, abcd and abcde.
I'm using the following command to find out the process id:
ps -ef | grep abc | grep -v grep
This gives the output for all the 3 processes with their corresponding pids:
user 6009 1 0 May 11 ? 0:23 ./abc
user 28047 1 0 Apr 24 ? 0:04 ./abcd
user 28548 1 0 Apr 27 ? 0:04 ./abcde
Now what I want is a grep thing that outputs the process id of just abc without returning abcd and abcde. I know using grep -v "processname" eliminates what i want but is there anything simple and specific?
ps -ef | grep -w abc | grep -v grep
^
`--- match whole words only
sure, there is:
pidof
Here is the man page: http://linux.die.net/man/8/pidof
For example write:
pidof abc
output:
6009
This is exactly what pgrep is for.
Specifically, for this you would use pgrep -x abc.
To avoid having to pipe into grep -v grep, do this:
ps -ef | grep '[a]bc\>'
the \> is an end-of-word boundary marker, so you won't match "abcd" or "abcde"
Putting one character into a bracket expression means that regex will match the string abc but it will not match the string grep [a]bc
I do this often enough that I wrote a function, psg
psg () {
local -a patterns=()
(( $# == 0 )) && set -- $USER
for arg do
patterns+=( "-e" "[${arg:0:1}]${arg:1}" )
done
ps -ef | grep "${patterns[#]}"
}
ps -ef | grep -w 'abc$' | grep -v grep
i.e. end of line ($) after abc
Related
I have certain tags stored in a series of .log files and i would like for the grep to show me Only the values > 31, meaning different to 0 and higher than 31
I have this code:
#! /bin/bash
-exec grep -cFH "tag" {} ; | grep -v ':[0-31]$' >> file.txt
echo < file.txt
Output:
I have this result from the grep:
/opt/logs/folder2.log:31
i was expecting not to bring nothing back if the result is 31 or less but still shows the result 31
i have also tried to add:
|tail -n+31
but didn't work
[0-31] means "0 or 1 or 2 or 3 or 1".
To drop all lines with 0-9, 10-19, 20-29, 30, and 31, you could use the following:
... | grep -ve ':[0-9]$' -e ':[12][0-9]$' -e ':3[01]$'
or as single regex:
... | grep -v ':\([12]\?[0-9]\|3[01]\)$'
With extended grep:
... | grep -vE ':([12]?[0-9]|3[01])$'
I tried to write a shell script that shows and counts how many suspended processes there are.
But I succeeded only to show the suspended processes with:
#!/bin/bash
list_ps=`ps aux | awk '$8~/T/'`
echo "$list_ps"
I tried to count the suspended processes with:
nr=0
for i in $list_ps
do
nr=`expr $nr + 1`
done
Of course this didn't work because it counted every word there was even with the first row that had the USER PID STAT COMMAND.
Can you give me any suggestion on how I should do it?
Also here is the output for "ps aux | awk '$8~/T/" after I stopped some sleep processes.
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
alexsan+ 6441 0.0 0.0 9008 736 pts/0 T 16:17 0:00 sleep 5000
alexsan+ 6511 0.0 0.0 9008 820 pts/0 T 16:18 0:00 sleep 5000
alexsan+ 7041 0.0 0.0 9008 760 pts/0 T 16:21 0:00 sleep 333
additional characters can be added to the state field (depending on the options you use), so this might be a safer approach:
ps aux | awk '$8~/T/'
to count how many processes you have with a header :
ps aux | awk '$8~/T/' | wc -l
to skip the header :
count=$(ps aux | awk '$8~/T/' | wc -l)
echo $((count -1))
one line version :
echo $(( $(ps aux | awk '$8~/T/' | wc -l)-1))
within single awk :
ps aux | awk 'NR>1 && $8~/T/' | wc -l
Using the option --no-header of the ps command, you will not need to substract 1 from the number of lines of output you get from wc -l, so you just have to issue:
ps --no-header aux | awk '$8 ~ /T/' | wc -l
And since the header will contain the 'T' of 'STAT' in field #8, it was necessary to filter this line out.
Within single awk could you please try following.
echo $(( $(ps aux | awk '$8~/T/{count++} END{print count-1}')))
Or to assign to a variable simply:
var=$(ps aux | awk '$8~/T/{count++} END{print count-1}')
With the below command:
# ps aux
USER PID %CPU %MEM SZ RSS TTY STAT STIME TIME COMMAND
root 17760468 0.0 0.0 1180 1220 - A 18:15:00 0:00 iostat -Dsal 6
root 16777310 0.0 0.0 3172 408 - A 18:15:00 0:00 /usr/bin/perl
here i don't want iostst -Dsal 6 under COMMAND, instead it should show only iostat. I mean only command is needed not with its all options and parameters.
Reason for this query:
I am trying to use this below command on my AIX box -
ps aux | tr -s " " | head -1 | awk '{print $1,$2,$3,$4,$11}' ; ps aux | sort -rn +2 | tr -s " " | head -10 | awk '{print $1,$2,$3,$4,$NF}'
Since i have used NF in awk command, it will display only the last column of the line. In my case it will display "6" instead of iostst -Dsal 6 under COMMAND field.
Please help me here..!
I have a file in Linux contains strings:
CALLTMA
Starting
Starting
Ending
Starting
Ending
Ending
CALLTMA
Ending
I need the quantity of any string (FE. #Ending, # Starting, #CALLTMA). In my example I need obtaining:
CALLTMA : 2
Starting: 3
Ending : 4
I can obtaining this output when I execute 3 commands:
grep -i "Starting" "/myfile.txt" | wc -l
grep -i "Ending" "/myfile.txt" | wc -l
grep -i "CALLTMA" "/myfile.txt" | wc -l
I want to know if it is possible to obtain the same output using only one command.
I try running this command
grep -iE "CALLTMA|Starting|Ending" "/myfile.txt" | wc -l
But this returned the total of coincidences. I appreciate your help .
Use sort and uniq:
sort myfile.txt | uniq -c
The -c adds the counts to the unique lines. If you want to sort the output by frequency, add
| sort -n
to the end (and change to -nr if you want the descending order).
A simple awk way to handle this:
awk '{counts[$1]++} END{for (c in counts) print c, counts[c]}' file
Starting 3
Ending 4
CALLTMA 2
grep -c will work. You can put it all together in a short script:
for i in Starting CALLTMA Ending; do
printf "%-8s : %d\n" "$i" $(grep -c "$i" file.txt)
done
(to enter the search terms as arguments, just use the arguments array for the loop list, e.g. for i in "$#"; do)
Output
Starting : 3
CALLTMA : 2
Ending : 4
I'm trying to make a server manager, but I need to grab the process IDs and Commands of some processes.
For example:
ps ax | grep ./skulltag
4760 pts/2 Tl 0:02 ./skulltag-server
4793 pts/2 Tl 0:01 ./skulltag-server
4956 pts/2 Tl 0:01 ./skulltag-server -port 13000
4958 pts/2 Tl 0:26 ./skulltag-server -port 13001
How would I get it to only return the process, only return the command (./skulltag-server) or both? Thanks.
You can pipe to awk to select which field to output
E.g. ps ax | grep ./skulltag | awk '{ print $1 }' will print the first column (pid)
Note that you may also want to look into using the -o option to ps to modify its output
I think you should use awk
ps ax | grep ./skulltag | awk '{print $1}' # Or $5, or $1 and $5
This will give you the list of the process ids.
For getting process ids you can also use non-standard but handy pgrep.
ps ax | grep ./skulltag | awk '{ print $1 }'
Is roughly equivalent to:
pgrep skulltag
You could parse the results of ps ax using awk to extract the columns you want:
aix#aix:~/tmp$ ps ax | grep bash
1906 pts/5 Ss+ 0:00 bash
13749 pts/31 Ss 0:00 bash
27315 ? SN 0:00 /bin/bash /etc/cron.daily/backup
27648 pts/31 S+ 0:00 grep --color=auto bash
aix#aix:~/tmp$ ps ax | grep bash | awk '{print $1}'
1906
13749
27315
27652
aix#aix:~/tmp$ ps ax | grep bash | awk '{print $5}'
bash
bash
/bin/bash
grep