From the course: Bash Patterns and Regular Expressions
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Using extended regexes in sed - Bash Tutorial
From the course: Bash Patterns and Regular Expressions
Using extended regexes in sed
- [Instructor] Now that we know the basics of how sed works, let's use regular expressions to manipulate text. I'll be using GNU sed for this exercise. In the chapter seven directory of the exercise files, I've included a system password file named passwd. We'll used this file with sed. Be sure you're in your chapter seven directory and type in ls. Now let's use sed to print lines of this file that match a pattern. This would be the same as grep. Type in sed -n '/root/p' passwd and hit enter. The pattern that we're matching is the word root, and the result is two lines. To narrow it down to one line that starts with root, we can use an anchor. Bring your line back, and insert a caret right before root, and hit enter. Now let's replace the word root with something else. Type in clear, and then type in sed -n 's, for substitute, /^root/toor, which is root backwards, /p' passwd, and hit enter. Note that we're substituting and by combining the -n option and the p substitution option…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.