Tips For Awk
- # prepend and append some text on each line of a text file:
awk '{print "prefix "$1" postfix"}' input.txt > /tmp/output.txt
- # replace the value of the second column in file1.dat by zero if it is strictly less than 0.1 (cfr., the gnuplot manual):
awk '{print $1,($2<0.1) ? 0.0 : $2}' file1.da
- # multiply the numeric value of the second column in file1.dat by five if the value of the first column is in the range of [1,3] (cfr., the gnuplot manual):
awk '{print $1,($1<=3 && $1>=1)? $2*5 : $2}' file1.dat
|

|
Tips For MyHowTos
- # text processing:
- # convert characters from lower to upper case:
fortune | tr [:upper:] [:lower:]
- # finding out how many times a particular item, in this case a dot, occurs on a text line
for a in `cat /tmp/file.txt`; do
nof=$[`echo $a|sed s/'\.'/'\n'/g|wc -l` -1]
echo a . occurs $nof times in $a
done
- # unescaping escaped text/html/javascript/... found in a file $LOGFILE:
cat $LOGFILE|sed s/+/' '/g|sed s/%0D%0A/" -- "/g|sed s/%20/" "/g|sed s/%21/'!'/g|\
sed s/%22/'"'/g|sed s/%23/'\#'/g|sed s/%24/"$"/g|sed s/%25/"%"/g|sed s/%26/"\&"/g|sed s/%27/"'"/g|\
sed s/%28/'('/g|sed s/%29/')'/g|sed s/%2A/'*'/g|sed s/%2B/'+'/g|sed s/%2C/','/g|sed s/%2D/'-'/g|\
sed s/%2E/'.'/g|sed s/%2F/'\/'/g|sed s/%3A/':'/g|sed s/%3B/';'/g|sed s/%3C/'<'/g|sed s/%3D/'='/g|\
sed s/%3E/'>'/g|sed s/%3F/'?'/g|sed s/%40/'@'/g|sed s/%5B/'['/g|sed s/%5C/'\\'/g|sed s/%5D/']'/g|\
sed s/%5E/'^'/g|sed s/%5F/'_'/g|sed s/%60/'`'/g|sed s/%7B/'{'/g|sed s/%7C/'|'/g|sed s/%7D/'}'/g|\
sed s/%7E/'~'/g|sed s/%D3/'<'/g
- # a very nice overview of commonly used text processing commands can be found at http://www.tldp.org/LDP/abs/html/textproc.html
- # remove all blank lines from a file:
grep -v '^$' input.txt > output.txt
- # count the number of newlines, words and bytes in a text file:
wc input.txt
- # reverse all the lines in a file:
rev file.txt
- # searching for specific patterns using grep and regular expressions (mostly copied from http://www.robelle.com/library/smugbook/regexpr.html):
- # the theory:
^ (Caret) = match expression at the start of a line, as in ^A.
$ (Question) = match expression at the end of a line, as in A$.
\ (Back Slash) = turn off the special meaning of the next character, as in \^.
[ ] (Brackets) = match any one of the enclosed characters, as in [aeiou].
Use Hyphen "-" for a range, as in [0-9].
[^ ] = match any one character except those enclosed in [ ], as in [^0-9].
. (Period) = match a single character of any value, except end of line.
* (Asterisk) = match zero or more of the preceding character or expression.
\{x,y\} = match x to y occurrences of the preceding.
\{x\} = match exactly x occurrences of the preceding.
\{x,\} = match x or more occurrences of the preceding.
- # examples:
cat -v -e -t dump # show non-printing characters too
grep BOB tmpfile # search 'tmpfile' for 'BOB' anywhere in a line
grep -i -w blkptr * # case insensitive search all files in the current directory for the word blkptr
grep run[- ]time *.txt # find 'run time' or 'run-time' in all txt files
grep smug files # search files for lines with 'smug'
grep '^smug' files # 'smug' at the start of a line
grep 'smug$' files # 'smug' at the end of a line
grep '^smug$' files # lines containing only 'smug'
grep '\^s' files # lines starting with '^s', "\" escapes the ^
grep '[Ss]mug' files # search for 'Smug' or 'smug'
grep 'B[oO][bB]' files # search for BOB, Bob, BOb or BoB
grep '^$' files # search for blank lines
grep '[0-9][0-9]' file # search for pairs of numeric digits
grep '^From: ' /var/spool/mail/$USER # list your mail
grep '[a-zA-Z]' # any line with at least one letter
grep '[^a-zA-Z0-9] # anything not a letter or number
grep '[0-9]\{3\}-[0-9]\{4\}' # 999-9999, like phone numbers
grep '^.$' # lines with exactly one character
grep '"smug"' # 'smug' within double quotes
grep '"*smug"*' # 'smug', with or without quotes
grep '^\.' # any line that starts with a Period "."
grep '^\.[a-z][a-z]' # line start with "." and 2 lc letters
- # file processing:
- # count the number of times a particular character (in the example a comma) occurs in some file:
cat file.txt | tr -c -d ',' | wc -c
- # character set translator -- translates character sets from one encoding to another (utf-8, ascii, iso 8859-[123456789], unicode, msdos,...):
tcs
- # make a hexdump or do the reverse:
xxd file > file.hex
xxd -r file.hex > file.bis
- # dump a file byte per byte using octet, decimal, hex or binary notation:
od -j 2 -t x1 file > file.hex
- # replace all spaces in all relevant file names by underscores:
find . -name \*' '\* -exec rename s/' '/_/g "{}" \;
- # strip the path from a filename, or the last part of an absolute path:
basename extendedFilename; dirname extendedFilename
- # find all files that are between 100 and 50 days old:
find . -mtime +50 -mtime -100 -daystart
- # process processing:
- # redirect stderr to stdout:
buggyProg 2>&1
- # redirect stdout to stderr:
buggyProg 1>&2
- # redirect both stderr and stdout to a file:
buggyProg &> file
|

|
Tips To Use Cvs
- # checkout of a cvs module from a cvs server with respect to a particular date:
cvs -z3 -d :pserver:cvs@cvs.server.org:/pub/cvsroot checkout -D "month dd yyyy" module
|

|
Tips To Use Diff And Patch
- # the following command produces (given two source trees ~/original/sources and ~/updated/sources) an overview of the differences between these two trees:
cd ~/original/sources
tar -xzvf ~/whatever.tgz
cd ~
diff -ruN original/sources updated/sources > how.to.patch.original.sources
- # the following command produces, given the original source tree and the differences between the original and updated sources, updated sources:
cd ~/current/sources
tar -xzvf ~/whatever.tgz
patch -p2 < how.to.patch.original.sources
|

|
Setting Up An Encrypted Filesystem
- # http://loop-aes.sourceforge.net/loop-AES.README
- # Encryption HOWTO
- # Encrypted Root Filesystem HOWTO
- # Cryptoloop HOWTO
- # Setting up the Cryptographic FileSystem (cfs):
- # setting the thing up:
- # install the cfs package:
sudo apt-get install cfs
- # create /etc/exports if it does not yet exist to avoid cfsd's complaints:
sudo touch /etc/exports
- # start the cfs daemon:
sudo /etc/init.d/cfsd restart
- # using the stuff:
- # prepare the creation of an encrypted directory called "~/encrDir":
cmkdir ~/encrDir
# the cmkdir command prompts for a strong passphrase (of at least 16 characters)
- # make this directory available to the user with that password:
cattach ~/encrDir decrypted
# the cattach command prompts for the passphrase that was used to create the directory ~/encrDir # once completed, the directory /crypt/decrypted becomes available wherein one can edit all files in plaintext
- # once you finish editing the decrypted data, this directory should be unmounted:
cdetach decrypted
# note that the content of the directory ~encrDir is at all times encrypted. The plaintext is only available to the user who presented the correct passphrase
|

|
Tips For Mysql
- # setting the server root password:
mysql -uroot mysql
mysql>UPDATE user SET Password=PASSWORD('mysqlrootpassword') WHERE user='root';
mysql>FLUSH PRIVILEGES;
mysql>quit
- # initial setting up of diogenes:
mysql -uroot -p"mysqlrootpassword"
mysql> use diogenes;
mysql> insert into diogenes_auth set username="root",password=MD5(""),perms="admin";
mysql> quit
|

|
Tips For Perl
- # look for a specific pattern in standard input (binary or text files) and print the pattern's byte-offset:
#!/usr/bin/perl
open(P,"-");
$a=join("",<P>);
while($a =~ m/pattern/gs){
print join(/:/,@-)."\n";
}
close(P);
- # find all common lines in two files (cfr., http://perlmonks.thepen.com/36725.html):
perl -ne 'print if ($seen{$_} .= @ARGV) =~ /10$/' fileA fileB >output
|

|
Tips For Sed
- # have sed fetch all lines from a text file starting from the sixth line:
sed -n '6,$p' file.txt
- # append line i to line i+1 if line i+1 starts with $PATTERN:
sed ':a;N;$!ba;s/\n$PATTERN/$PATTERN/g' file.txt
- # a list of handy one-liners for the unix stream editor sed can be found at http://www.student.northpark.edu/pemente/sed/sed1line.txt
|

|