School of Computing. Dublin City University.
Online coding site: Ancient Brain
coders JavaScript worlds
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 string: 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".
In July 2019, poor
use of regular expressions in a firewall rule
took down
Cloudflare,
taking down a big chunk of the Internet.
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- Note: ls -l outputs are not actually on predictable columns. Longer/shorter userids give different column numbers. Try: ls -l /
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 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|"
'|' is just my choice of a separator. Other people like '/' We can actually use any character as a separator (whatever comes first after "s").
(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" |
cat file | tr ' ' '\n'
tr -d '\015' # remove 15 octal, also known as "return" char "\r" # see ASCII chars
cat -tve file
cd /users/tutors/mhumphrysdculab/share/testsuite # find all files with Windows EOL character: grep -l '\015' */*html # find all files with characters other than the basic 7 bit characters (00 to 7E hex): LC_ALL=C grep -P "[^\x00-\x7E]" */*html # if you grep a file with non 7 bit chars you may get a warning like: grep: file.html: binary file matches
dirname basename$ echo $HOME /users/group/me $ dirname $HOME /users/group $ basename $HOME me $ dirname `dirname $HOME` /users
grep string files | head -100
grep string files | head -100When head gets the first 100 lines, the pipe closes and grep terminates.
yes | head -20
tail
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" 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:
date "+%H.%M.%S.%N"
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
For sending:
# bundle directory into one file tar -cf dir.tar dir # compress it gzip dir.tar # (can actually do the above two in one step)
When receiving:
# de-compress it gzip -d dir.tar.gz # un-tar it tar -xf dir.tar # (can actually do the above two in one step)