|
|||
Department of Engineering | |
University of Cambridge > Engineering Department > computing help |
grep Kwan /etc/passwd
'
prints all lines in /etc/passwd that include `Kwan'. The
return value indicates whether any such lines have been found.
ls | sed s/a/o/g
will
produce a listing where all the 'a' characters become 'o'. Numerous
tricks and tutorials are available from the
Handy one-liners for SED
file.
echo "date" | tr d lThe following does case conversion
echo LaTeX | tr '[:upper:]' '[:lower:]'You can also use it as follows, ``capturing" the output in a variable
old="LaTeX" new=$(echo $old | tr '[:upper:]' '[:lower:]')
if [ -f data3 ]
'
is equivalent to ` if test -f data3
'; both check for a
file's existence, but the [...]
construction is faster - it's a
builtin. Note that you need spaces around the square brackets.
Some examples,
if [ $LOGNAME = tpl ] # Did the user log in as tpl? if [ $num -eq 6 ] # Is the value of num 6? if [ $(hostname) = tw000 ] # Does hostname produce `tw000'? # i.e. is that the machine name?
ls -l | sort -nr +4
'
lists files sorted according to what's in column 4 of ls -l's output (their size, usually).
cut -f1 -d':' < /etc/passwd
prints all the uid's in the passwd
file. The following code shows a way to extract parts of a filename.
filename=/tmp/var/program.cc b=$(basename $filename) prefix=$(echo $b | cut -d. -f 1) suffix=$(echo $b | cut -d. -f 2)