The information about the CPU is stored in /proc/cpuinfo. The following command will show you the number of processors by reading that file:
$ grep processor /proc/cpuinfo | wc -l
A simple
$ grep -c processor /proc/cpuinfo
should also be enough!
16 Saturday Jan 2010
The information about the CPU is stored in /proc/cpuinfo. The following command will show you the number of processors by reading that file:
$ grep processor /proc/cpuinfo | wc -l
A simple
$ grep -c processor /proc/cpuinfo
should also be enough!
31 Saturday Oct 2009
Tags
cli, eject, ejection, grep, htop, linux, lsof, mac, macosx, osx, processes, ps, top, unix, volume
I sometime have problem finding which persistent process is not letting me eject a volume (CD/DVD/USB drive). The error message is not very helpful at all:
Try quitting applications
OK, but which one(s)? I often have hard time figuring that out using htop/ top or a combination of ps and grep.
But actually this is what I want:
$ lsof | grep -i Volume_Namewhere Volume_Name is the name of the volume (or an ‘identifying’ part of the name) I want to eject. This gives me the name(s) and process ID(s) of the run-away process(es) as well as the path to the file(s) in use on the volume, Volume_Name.
Now that I know the names I should first try to save the documents and quit the applications involved in the normal way. However, if a particular application does not oblige, I can always kill it (with the risk of potential data loss!) using its process ID (say, 123):
$ kill -9 123After that, the ejection of the volume should not be difficult at all.
Needless to say, being a Unix utility, lsof (=“list open files”) may be used on other *nix-based systems as well.
26 Saturday Sep 2009
Posted in awk
Print columns 1, 5 and then 2, put a colon, and then print column 10 of a line that matches pattern from the file columns.txt:
$ awk '/pattern/ {print $1, $5, $2, ": ", $10}' columns.txt
.
Protecting the special meaning of the single quotes and curly braces
I came across this situation when I was trying to watch the above command as well as two other commands (say, command1 and command2) at the same time. The correct way to do that is by properly protecting the meaning of the special characters in the above awk invocation:
$ watch -d -n 20 "command1; command2; awk '"'/pattern/ {print $1, $5, $2, ": ", $10}'"' columns.txt".
N.B. The “-d”flag highlights the changes, whereas the “-n 20” flag causes the above to watch every 20 seconds.
Just for the heck of it, let me put the full command that I was actually using:
$ watch -d -n 20 "ps aux | grep -i columbus | grep -v grep && echo; tail WORK/ciudgsm && echo && grep bond output.log | tail -13 && echo && awk '"'/state # 1/ {print $3,$4,$5,": ", $10}'"' output.log && echo; awk '"'/state # 2/ {print $3,$4,$5,": ", $10}'"' output.log && head curr_iter"
(suggestions for making the above shorter, other than by aliasing, are most welcome!).
Reference: here.
26 Saturday Sep 2009
I always get confused about various flavors of grep. Here’s a summary to shine some light: (man grep to know more!)
egrep or grep -E (in linux only) is extended grep where additional regular expression metacharacters have been added like +, ?, | and ()fgrep or grep -F (in linux only) is fixed or fast grep and behaves as grep but does not recognize any regular expression metacharacters as being special.25 Friday Sep 2009
For grepping line-by-line in a file filename, I often find these very useful
Match pattern1 OR pattern2 in the same line:
$ grep -E 'pattern1|pattern2' filename
Match pattern1 AND pattern2 in the same line:
$ grep -E 'pattern1.*pattern2' filename
The above command searches for pattern1 followed by pattern2. If the order does not matter or you want to search them in either order, then use the follwoing
$ grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename
The pipe enables the OR search which we saw earlier. Another option for this situation (i.e., AND search when the order is not important):
$ grep -E 'pattern1' filename | grep -E 'pattern2'
which basically greps the STDOUT of the first grep.
Match pattern1 AND pattern2, but NOT pattern3 in the same line:
$ grep -E 'pattern1.*pattern2' filename | grep -Ev 'pattern3'
when the order of the first two patterns is important. When that order is NOT important:
$ grep -E 'pattern1' filename | grep -E 'pattern2' | grep -Ev 'pattern3'
Match pattern1 OR pattern2, but NOT pattern3 in the same line:
$ grep -E 'pattern1|pattern2' filename | grep -Ev 'pattern3'
N.B. (1) grep -E may be replaced by egrep. I used grep -E everywhere in this post assuming a general case of regular expressions as patterns. Lowercase -e is also used for regex, but this is more “basic” than -E which supports “extended” regex, e.g. regular expression metacharacters like +, ?, | and (). (2) The -v flag is for non-matching grep.