[zsh] Histroy substitution

Tags

, ,

#For Previous Command (for comparison)
!-1     repeat whole command
!!      repeat (shortcut)
!:0     command
!^      first parameter
!:1     first parameter
!:1-4   first 4 parameters
!$      last parameter
!*      all parameters
!!:s/bash/zsh (or ^bash^zsh)
!^:t    just file name of first parameter
!$:h    just path of last parameter
!-2$:r  just file name without extension of first parameter

For last but one command
!-2     repeat last but one command
!-2^    first parameter last but one command
!-2$    last parameter last but one command
!-2:2   second parameter of second but last command
!-2:s/bash/zsh substitute bash by zsh in the last but one command


Most of them are applicable for bash as well.

Credit: copied from here.

[zsh] Command line math functions

Tags

, , , ,

$ zmodload zsh/mathfunc
$ echo $(( sin(1/4.0)**2 + cos(1/4.0)**2 - 1 ))
  -1.1102230246251565e-16
$ echo $(( pi = 4.0 * atan(1.0) ))
  3.1415926535897931
$ echo $(( f = sin(0.3) ))
  0.29552020666133955
$ print $((1e12 * rand48()))
  847909677310.23413
$ print $(( rand48(seed) ))
  0.0104348833470027

Notice that zsh/mathfunc must be loaded first!

Credit: copied from zsh-lovers.

[perl] Search and replace text in multiple files from the command line

Tags

, , , ,

$ perl -pi -w -e 's/foo/bar/g;' *.txt

-e means execute the following line of code.
-i means edit in-place
-w write warnings
-p loop

It will search for “foo” in files with extension .txt and replace it with “bar”.

Credit: here.

[unix] Determine who is monopolizing the CPU

Tags

, , , , , ,

$ ps -eo pcpu,size:8,user,start_time,pid,args | sort -nk 1 -r |egrep -v "COMMAND|egrep -v|start_time"| head -10

The ps command displays every process (-e) with user-defined format (e.g., -o pcpu).

pcpu = CPU usage percentage
size = memory size in kB (with 8 character long field — otherwise the formatting is messed up!)
user = user name
start_time = start time
pid = process ID,
args = command being run

The sort command sorts the lines numerically (-n) in the reverse order (-r) based on the first field (-k 1).

Then egrep -v filters the lines that do not match the header (containing COMMAND), egrep and ps commands themselves.

The head -10 command filters out top 10 lines from the list generated by previous sort. (Note: here -10 is actually redundant since head command prints out top 10 lines by default, but it is there as a reminder that we can print more lines using a number greater than 10  it if there is a need!)

Credit: Nixcraft.

[unix] Display the number of processors in the command line

Tags

, , , , , , , , ,

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!

Design a site like this with WordPress.com
Get started