-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
## Deleting lines matching a pattern containing slashes | ||
|
||
I want to delete a line from a file, matching a string. | ||
This is a simplified example. | ||
|
||
$ string='a line with spaces and a $variable substitution' | ||
$ echo $string | ||
a line with spaces and a $variable substitution | ||
$ echo $string > temp.txt | ||
~ $ sed "/$the_string/d" temp.txt | ||
~ $ | ||
|
||
So far so good. | ||
|
||
However, the actual string in the real problem is this: | ||
`export PATH="$PATH:/home/mbexegc2/.local/share/hpcflow/links"` | ||
|
||
So I can't use forward slash as the delimiter without escaping all the slashes | ||
in the string. | ||
I thought I could just use a different delimiter (which I've done in the past with | ||
sed (search/replace) substitutions). | ||
However, it doesn't work: | ||
|
||
~ $ sed "#$the_string#d" temp.txt | ||
a line with spaces and a $variable substitution | ||
|
||
Turns out, you *can* use a different delimiter, but you need to escape it with a backslash | ||
at the start: | ||
|
||
~ $ sed "\#$the_string#d" temp.txt | ||
~ $ | ||
|
||
|