Chang the swap behavior of linux

The default swappiness is 60. It can be 0-100, with the bigger value more pages will be swapped to harddisk from memory.
If you want to use more physical memory and less swap, decrease the number.

echo 40 > /proc/sys/vm/swappiness
Share

What you may not know about linux sed

1. Separator can be any symbol.

This also works:

echo '/etc/python3/dummy' | sed 's|/etc/python3/|/etc/python2/|'

output:

/etc/python2/dummy

In this example above I used | to replace the normally used separator /

Share

Two ways to write literal strings containing line breaks to a file

1. Use printf

printf "line 1
line2
line 3 containing a variable: ${PWD}
line 4 contains a quote mark ' and \"
">temp.txt
 
cat temp.txt

Notice double quote ” needs to be escaped.

2. Use Here Doc:

cat > temp.txt <<EOF
line 1
line2
line 3 containing a variable: ${PWD}
line 4 contains a quote mark ' and "
EOF
cat temp.txt

Notice double quote ” doesn’t need to be escaped.

Also notice “EOF” can actually be anything. This also works

cat > temp.txt <<SEPARATOR
line 1
line2
line 3 containing a variable: ${PWD}
line 4 contains a quote mark ' and "
SEPARATOR
cat temp.txt

Notice double quote ” doesn’t need to be escaped.

http://en.wikipedia.org/wiki/Here_document#Unix-Shells

Share

split string in Bash

emails="email1@gmail.com;email2@gmail.com;email3@gmail.com"
emails=${emails//;/ } #replace ; with space
arr_emails=($emails) # put them into an array
for item in ${arr_emails[@]}; do
  echo $item
done

In fact this works also. You don’t really need arrays

emails="email1@gmail.com;email2@gmail.com;email3@gmail.com"
emails=${emails//;/ } #replace ; with space
for item in $emails; do
  echo $item
done

But with using arrays, you can do many other operations http://www.thegeekstuff.com/2010/06/bash-array-tutorial/

For the bash or sh version that “emails=${emails//;/ } ” doesn’t work, use tr command:

emails="email1@gmail.com;email2@gmail.com;email3@gmail.com"
emails=$(echo $emails | tr ";" " ")
for item in $emails; do
  echo $item
done

This also works:

emails="email1@gmail.com;email2@gmail.com;email3@gmail.com"
emails=$(echo $emails | tr ";" "\n")
for item in $emails; do
  echo $item
done
Share

sed regular expressions

echo 'iweigh297lbs' | sed 's/.*[^0-9]\([0-9]\+\).*/\1/'

The expression has to match the whole line.
Greed: The expression will match the largest possible from left to right.
This won’t work.

echo 'iweigh297lbs' | sed 's/.*\([0-9]\+\).*/\1/'

Output 7

This won’t work

echo 'iweigh297lbs' | sed 's/.*[^0-9]\([0-9]*\).*/\1/'

This works

echo 'iweigh297lbs' | sed 's/.*[^0-9]\([0-9][0-9]*\).*/\1/'

+ has to be escaped. * should not be escaped.

Share