//Tips tagged cut
Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
Want to check the amount of used, free and total memory and swap from the command line? This script displays memory and swap information. Fully posix compliant and should work with all 2.[2-6].* kernels .

#fetch and process memory information
[ -f /proc/meminfo ] && {
Buffers=`grep -we 'Buffers' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
Cached=`grep -we 'Cached' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
MemFree=`grep -ie 'MemFree' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
MemTotal=`grep -ie 'MemTotal' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
SwapCached=`grep -ie 'SwapCached' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
SwapFree=`grep -ie 'SwapFree' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
SwapTotal=`grep -ie 'SwapTotal' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
}
MEMUSED="$(( ( ( ( $MemTotal - $MemFree ) - $Cached ) - $Buffers ) / 1024 ))"
MEMTOTAL="$(( $MemTotal / 1024))"
MEMFREE="$(( $MEMTOTAL - $MEMUSED ))"
MEMPER="$(( ( $MEMUSED * 100 ) / $MEMTOTAL ))"
[ "$SwapTotal" -gt "1" ] && {
  SWAPUSED="$(( ( ( $SwapTotal - $SwapFree ) - $SwapCached ) / 1024 ))"
  SWAPTOTAL="$(( $SwapTotal / 1024))"
  SWAPFREE="$(( $SWAPTOTAL - $SWAPUSED ))"
  SWAPPER="$(( ( $SWAPUSED * 100 ) / $SWAPTOTAL ))" 
} || {
  SWAPUSED="0"
  SWAPTOTAL="0"
  SWAPPER="0" 
}

# display the information
/bin/echo
/bin/echo "Memory"
/bin/echo "Used: $MEMUSED"
/bin/echo "Free: $MEMFREE"
/bin/echo "Total: $MEMTOTAL"
/bin/echo
/bin/echo "Swap"
/bin/echo "Used: $SWAPUSED"
/bin/echo "Free: $SWAPFREE"
/bin/echo "Total: $SWAPTOTAL"
/bin/echo


View Comments »



I use the following to list non-system users. It should be portable though won't work on systems without the getent command.

alias lsusers='getent passwd | tr ":" " " | awk "\$3 >= $(grep UID_MIN /etc/login.defs | cut -d " " -f 2) { print \$1 }" | sort'


View Comments »

cut -d"," -f1,5 file.csv > newfile.csv

Will return only the 1st and 5th columns of a comma-delimited csv file.


View Comments »

My little "iso2cd" alias. Not clean, but handy. The Burning device will be auto detected.

example call:
iso2cd debian_lenny_final.iso

alias iso2cd="cdrecord -s dev=`cdrecord --devices 2>&1 | grep "\(rw\|dev=\)" | awk {'print $2'} | cut -f'2' -d'=' | head -n1` gracetime=1 driveropts=burnfree -dao -overburn -v"


View Comments »

SSH_COMPLETE=( $(cat ~/.ssh/known_hosts | \
                 cut -f 1 -d ' ' | \
                 sed -e s/,.*//g | \
                 uniq | \
                 egrep -v [0123456789]) )
complete -o default -W "${SSH_COMPLETE[*]}" ssh


View Comments »

The 'read' command can be used to split arguments based on any arbitrary character using IFS:

$ echo 'a/bc/ de fg hi/j' | { IFS='/' read first second others; echo "$first"; echo "$second"; echo "$others"; }
a
bc
 de fg hi/j


View Comments »




Home Latest Browse Top 25 Random Hall Of Fame Contact Submit