Wednesday, June 22, 2005

sed: Swap two words around

$ cat test
first:second
one:two
$ sed 's/\(.*\):\(.*\)/\2:\1/' test
second:first
two:one

sed: Enclose a word in angle-brackets

s/html/<&>/


returns ""

Saturday, June 18, 2005

Delete all lines in a file that begins with 'O'

$cat somefile | sed '/^O/d'


Friday, June 17, 2005

Strip off file extension

${filename%.*}


Example:
$ filename="Foo.java"
$ echo ${filename%.java}
Foo



Or

$ filename=$(basename Foo.java .java)

Sunday, June 12, 2005

Set cdable directories

In .bashrc:

shopt -s cdable_vars
export bin=~/bin
export doc=~/files/MyDocs


*Note: It doesn't seem to work if it's in .bash_profile.

Friday, June 10, 2005

Searching the command history

1) Press Ctrl-R at the shell
2) Begin typing a few letters of the command containing the string you typed so far
3) Press Enter to execute the command

Command subsitution

Assign command output to a variable:


$ today=(date +%d-%b-%Y)


this is similar to the older Bourne-style syntax like:


$ today=`date +%d-%b-%Y`

Brace Expansion

Create a copy/backup of a file:


$ cp some.config{,.bak}


is like typing:


$ cp some.config some.config.bak