1/13
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
what does grep do, its usage format, and its 6 options?
looks for patterns from files or standard input
usage: grep -flags ‘regex’ file
options:
-h to not display filenames
-i to ignore cases
-l to list only filenames containing the pattern
-n precede each matching line with its line number
-v negate matches
-o print only the matched (non-empty) parts
what regex does grep use by default and can you change it?
it uses BRE by default. you can change it to ERE by doing grep -E blah
by default, when you enter in multiple files to grep, how does it print the results?
it does filename:line , where line are the lines with matches, and those matches are highlighted.
what does -l option do?
when you’re searching for something with grep, it returns all lines which match. this option makes it so that it only returns the name of the file it found the match in… so if you put in 5 files, and there are matched lines in only 3 of them, it will return the names of those 3 files.
what is sed and its use format?
it’s basically a text editor. looks for patterns, processes lines, etc.
format: sed command/script file(s)
command is either a regex pattern with format /pattern/ or a line number. there can be up to TWO commands (also called addresses FML)
script is the action you are performing on it.
what does sed do to the original files and where and where is sed’s output?
it does not change the original file. by default, its output is to stdout, but it can be redirected.
what are the three ways you can provide addresses and what is the format they’re provided in?
format: comma separated.
1,2n
/pattern/,/pattern/d
no address: command is appliped unconditionally to all lines
one address: command is applied when address matches the line
two addresses: command is applied to the range of addresses
if numbers, then FROM this line TO that line
if patterns, then FROM this pattern TO that pattern.
what are all of sed’s scripts?
p print
= print line number
d delete
s substitute
y transform
q quit
what is sed’s default output and how do you change it?
by default, it prints EVERYTHING it reads to stdout. use -n flag after saying sed to force it to only print what you asked for.
how do you perform a sed action (for ex, deleting) on two separate patterns or line with one command?
use chaining using -e between command/script .
so like this:
sed -e '/pattern1/d -e /pattern2/d filename
for sed’s substitute script, what is the format?
[address(es)]s/old/newreplacement/OPTIONAL-SUBSTITUION-FLAG .
addresses are either regex patterns or line num
s specifies substitution happening
old is the thing you want gone and replaced (can enter regex)
newreplacement is what old is replaced with (only literal strings, no regex, except backreferences which are allowed.
OPTIONAL-SUBSTITUTION-FLAG is to specify how many occurences you want changed. by default, it is first occurrence. g makes it all. any other number makes it so that it targets that n’th index occurrence. p prints the result contents.
for sed’s transform script, what is the format?
[address(es)]y/src/dst/
addresses are either regex pattern or line num
y specifies transformation happening
src is characters we’re gonna transform
dst is characters that src characters are being transformed to, and IT IS BY POSITION! so /ab/BB means all a→B, and all b→B.
how many occurrences does sed’s transform script change?
transform is global only. it will change literally every match in specified lines/matching regex.
how does sed’s quit script work?
it terminates the sed command so it doesn’t finish the whole file. you can specify a pattern or linenum to get it to terminate early.