Quantifiers:
? 0 or 1 occurrences of the preceding text
* 0 or N occurrences of the preceding text (N > 0)
+ 1 or N occurrences of the preceding text (N > 1)
Grouping:
(text) Grouping of text (used either to set the borders of an alternative as above, or to make backreferences, where the Nth group can be referred to on the RHS of a RewriteRule as $N)
Anchors:
^ Start-of-line anchor
$ End-of-line anchor
I will use grep to demonstrate, but there are other programs that could be used.
The basic syntax is grep regex file
. Below I will just write the regex.
^aa
words that start with an 'aa'aa$
words that end with 'aa'Check out the following Tutorial:
More Optional Reading
Substitution
Replaces all instances of day with night inside myfile.txt. Note the g at the end.
sed 's/day/night/g' myfile.txt
Removing stuff
Do not print the first line
sed '1d' file.txt
Remove the first character of every line
sed 's/^.//' file
Remove the last character of every line
sed 's/.$//' file
Remove lines 7 thru 9 of a file
sed '7,9d' filename.txt
Remove the line containing the string Fred
sed '/Fred/d' filename.txt
Print every nth line beginning with in the file
Print only the first line
sed -n '1p' file.txt
Print every third line starting from line 3 (which will print lines 3, 6, 9, etc)
sed -n '0~3p' file.txt
Print every fifth line starting with the second line (which will print lines 2, 7, 12, 17, etc)
sed -n '2~5p' file.txt
Print a range of lines
Print lines 1 through 4
sed -n '1,4p' file.txt
Print lines 2 through 4
sed -n '2,4p' file.txt
Read the following webpage:
Optional Reading
Awk is hugely powerful, but we will just look at how it can be used for text pattern scanning.
It is also often used with Regular Expressions.
Here are some examples:
print the first and third columns in the output of a command
ls -l | awk '{ print $1 $3 }'
print the first column in the output of a command
ps aux | awk '{ print $1 }'
print the first column in the output of a command and add text.
ps aux | awk '{ print "this process is owned by " $1 }'
print first column of values in file separated by :
awk -F: '{ print $1 }' /etc/passwd
print second and eighth column separated by ;
awk -F';' '{ print $2, $8 }' master_file_room_104.dhcp
print first column of values in a file
awk '{ print $1 }' /etc/fstab
Using regular expressions search for lines that start with UUID and print 3rd column of results
awk '/^UUID/ {print $3}' /etc/fstab
Figure this one out - if you don't know what the lspci -v
command does - read the man page.
lspci -v | awk '/VGA/ { print $6 }'
Awk does Arithmetic operations too (assuming grades is a file with 3 scores on each row)
awk '{ print "the average is ", ($1+$2+$3)/3 }' grades
Here is a basic tutorial
Optional Advanced Awk Reading