School of Computing. Dublin City University.
My big idea: Ancient Brain
grep search for a string grep string file (output of prog) | grep string | grep otherstring -i ignore case -v return all lines that do NOT match
|More complex solution to long-line problem:|
^ start-of-line $ end-of-line . any character where "c" stands for the character: c* 0 or more instances of c cc* 1 or more instances of c grep " *" 1 or more spaces .* any sequence of characters where "c" has a special meaning, e.g. is $ or ., etc: \c the character itself grep "\." the '.' character itself recall the two forms of quote: grep '\$' works (searches for the "$" char instead of end-of-line) grep "\$" fails (double quote treatment of $ is different to single quote treatment of $) grep "\\$" works
Find all lines containing "born" at start of line.
Find all lines containing start of line, then one space, then "born".
Find all lines containing start of line, then any number of spaces, then "born".
cut extract columns or fields of text on command-line To extract columns 30 to end of line of the ls listing: ls -l | cut -c30- In grep output, extract the 1st field, with delimiter ":" grep string *html | cut -f1 -d':' Extract the 2nd to end fields, with delimiter ":" grep string *html | cut -f2- -d':'Q. Why "-f2-" ?
sed "stream editor" - find and replace text on command-line (and other things) sed 's|oldstring|newstring|' change first match on each line sed 's|oldstring|newstring|g' change all matches '|' is just my choice of a separator. Other people like '/' We can actually use any character as a separator (whatever comes first after "s"). e.g. ls listing that highlights web pages: ls -l | sed "s|\.html| [Web page]|" e.g. ls listing that changes how my username appears: ls -l | sed "s|$USER|ME|"
(recognises "\n")sed 's|www|\nwww|g'
sed 's|www|\ www|g'
sed 's|<|\n<|g' | sed 's|>|>\n|g'
sed 's|<|\ <|g' | sed 's|>|>\ |g'
# \( ... \) to mark a pattern # \1 to reference it later # e.g. change: # (start of line)file.html: ... # to: # <a href=file.html>file.html</a>: ... # search for: # ^\(.*\.html\): # change to: # <a href=\1>\1</a>: grep -i $1 *html | sed -e "s|^\(.*\.html\):| <a href=\1>\1</a>: |g"
tr - character substitutions change spaces to new lines: cat file | tr ' ' '\n'
awk - a powerful pattern scanning and processing language
dirname basename$ echo $HOME /users/group/me $ dirname $HOME /users/group $ basename $HOME me $ dirname `dirname $HOME` /users
grep string files | head -100
yes | head -20
cat logfile | tail -30
date looks like: "Tue Feb 17 16:28:33 GMT 2009" CURRENTDATE=`date` remember backquotes echo $CURRENTDATE date "+%b %e" looks like: "Jan 21" (see "man date") date "+%b.%e.log" can add things to the string file=`date "+%b.%e.log"` echo $file
timenow=`date +%H%M%S` filename="/tmp/random.$timenow.txt"Unique to second and nanosecond:
filename=/tmp/random.$$.txtWould be different for each process.
echo $RANDOMThis is a strange environment variable. It does not exist until you try to access it. Then it exists!
set | grep -i random echo $RANDOM set | grep -i random echo $RANDOM
About random-number generators:
A pseudo-random-number generator
typically generates a series of random-looking numbers based on a start seed.