cs288 grep and sed

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/13

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 3:35 AM on 10/16/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

14 Terms

1
New cards

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


2
New cards

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

3
New cards

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.

4
New cards

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.

5
New cards

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.


6
New cards

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.

7
New cards

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


  1. no address: command is appliped unconditionally to all lines

  2. one address: command is applied when address matches the line

  3. two addresses: command is applied to the range of addresses

    1. if numbers, then FROM this line TO that line

    2. if patterns, then FROM this pattern TO that pattern.



8
New cards

what are all of sed’s scripts?

  • p print

  • = print line number

  • d delete

  • s substitute

  • y transform

  • q quit


9
New cards

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.

10
New cards

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

11
New cards

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.


12
New cards

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.


13
New cards

how many occurrences does sed’s transform script change?

transform is global only. it will change literally every match in specified lines/matching regex.

14
New cards

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.