SED and regular expressions
We have already learned REGEX. Let’s learn the use of SED + REGEX
by using a couple of practical exmaples:
Example 1: match patterns and REGEX (\!.*
)
$ sed '/\!/s/\!.*//g' datafile
In this example, if the line matches with the pattern “!“
, then it replaces all the characters from “!”
with an empty char.
Example 2: use REGEX to delete the last x
characters
$ sed 's/...$//' datafile
This sed example deletes last 3 characters from each line.
Example 3: use REGEX to eliminate comments (#
)
$ sed -e 's/#.*//' datafile
This sed example deletes last 3 characters from each line.
Example 4: use REGEX and eliminate Comments (#
) as well as the empty lines
$ sed -e 's/#.*//;/^$/d' datafile
Get hands-on with 1400+ tech skills courses.