//Tips tagged tr
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 »



The command below can be used to convert a string into a valid filename. Uppercase letters are converted to lowercase, all difficult characters are removed and spaces are changed to underscores.

echo "#cLEa:n (%FILE%)" | tr [:upper:] [:lower:] | tr -d "[]()&~@#%^&*()_+=;:,\$" | tr " " "_"

This example will output 'clean_file'.


View Comments »

The 'tr' command can be used to quickly convert uppercase to lowercase and vice versa. This is done with:

tr [:upper:] [:lower:]

To give an example this can be used to rename all files in the current directory to lowercase (there are other ways to do this, but this is a decent example):

for file in * ; do echo mv "$file" "`echo $file | tr [:upper:] [:lower:]`"; done

Of course you can convert the other way by swapping '[:upper:]' and '[:lower:]'.


View Comments »

Ever wanted to look at a long HTML URL more easily, say to investigate a parameter. Here is a search from MapQuest.

http://www.mapquest.com/maps/map.adp?formtype=address&addtohistory=&address=10%20Market%20St&city=San%20Francisco&state=CA&zipcode=94111

We can split this using:

$ echo "[insert url here]" | tr "&?" "\n"

This produced for the above URL the following output.

http://www.mapquest.com/maps/map.adp
formtype=address
addtohistory=
address=10%20Market%20St
city=San%20Francisco
state=CA
zipcode=94111


View Comments »




Home Latest Browse Top 25 Random Hall Of Fame Contact Submit