sed: How to delete lines matching a pattern that contains forward slashes?
You were very close. When you use a nonstandard character for a pattern delimiter, such as |pattern|
, the first use of that character must be escaped:
$ sed '\|^/dev/xvdb|d' /etc/fstab
/dev/xvda1 / ext4 defaults 1 1
/dev/md0 /mnt/ibsraid xfs defaults,noatime 0 2
/mnt/ibsraid/varlog /var/log none bind 0 0
Similarly, one can use:
sed '\?^/dev/xvdb?d' /etc/fstab
Lastly, it is possible to use slashes inside of /pattern/
if they are escaped in the way that you showed in your answer.
using sed to delete lines containing slashes /
You can use still use alternate delimiter:
sed '\~//~d' file
Just escape the start of delimeter once.
sed: Replace lines matching a pattern that contains forward slashes?
In your command, you are missing s
for substitution and have wrongly escaped \
character. Also as you replied to my comment, that you want to replace it from anywhere in the file, you don't have to use ^
character in your regex. And dot .
in regex means any character so they need to be escaped too.
You can use this command,
sed -i 's/OPP\/com\.user\.opp\.orchest\.po\.services\.stub-npo\/npo-stub/OPP\/com.user.opp.orchest.po.services.stub-ica\/npo-ica/g' yourfilename
Delete lines containing pattern at the end of line
There are two approaches here, either print all non-matching lines with
sed -in '/LOCATION=$/!p' file
or delete all matching names with
sed -i '/LOCATION=$/d' file
The first uses the n command line option to suppress the default action of printing the line. We then test for lines that end in LOCATION=
and invert the pattern (only keeping those that don't match). When we get a desirable line, we print it with the p option.
The second looks for lines matching the end of line pattern, and deletes those that do.
Your file contains blank lines, and both of these keep those. If we don't want to keep those, we can change the first option to
sed -in '/^$/!{/LOCATION=$/!p}' file
which first checks if a line is not empty, and only bothers checking if it should be printed if it isn't empty. We can modify the second option to
sed -i '/^$/d;/LOCATION=$/d' file
which deletes blank lines and then checks about deleting the other pattern.
We can modify the options to work with different line ending by specifying the difference in the pattern. The difference between line endings on Unix/Linux (\n
) and Windows (\r\n
) is the presence of an extra carriage return on Windows. Modifying the four commands above to accept either, we get
sed -in '/LOCATION=\r\{0,1\}$/!p' file
sed -i '/LOCATION=\r\{0,1\}$/d' file
sed -in '/^\r\{0,1\}$/!{/LOCATION=\r\{0,1\}$/!p}' file
sed -i '/^\r\{0,1\}$/d;/LOCATION=\r\{0,1\}$/d' file
Note that in each of these we allow an optional \r
before the end of line. We use the curly bracket notation, as sed does not support the question mark optional quantifier in normal mode (using the r option to GNU sed for enabling extended regular expressions, we can replace \{0,1\}
with ?
).
On a Windows shell, all of the options above require double quotes instead of single quotes.
How can I remove the text between the first and last slash in a string using sed?
sed
does not support the various Perl regex extensions you tried to use. But really all you need is
sed 's:/.*/:/:'
Regular expressions perform longest-leftmost matching, so /.*/
by definition matches from the first slash to the last.
"Why doesn't tool X support the regex dialect of tool Y" is a common FAQ; see e.g. Why are there so many different regular expression dialects? but really, these online regex testers shuld be more explicit about which tools they support.
SED: delete lines after pattern match while skipping a few lines, then delete the pattern line
Another approach:
sed '/($(ose),)/,/^endif/{/($(ose),)/d;/^else/,/^endif/d}' file
Output:
...
dh_installdocs \
$(archdir)/UserManual*.pdf $(archdir)/VirtualBox*.chm \
$(addprefix $(archdir)/, LICENSE)
rm $(addprefix $(archdir)/,UserManual*.pdf VirtualBox*.chm \
LICENSE)
...
Add option -i
to edit "in place".
How to use sed to delete lines which have certain pattern and at some specific line range?
sed "50,${/^\s*PUTS/d}" the_file
this line won't work, because you used double quotes, and you need escape the dollar: \$
or use single quote: '50,${/.../d}' file
sed "50,99999{/^\s*$pattern/d}" file
this line should work.
EDIT
wait, I just noticed that you set env var via set
... this is not correct if you were with Bash. you should use export PAT="PUT"
in your script.
check @Jonathan and @tripleee 's comments
Related Topics
What Is File Hole and How Can It Be Used
How to Run a Command as a Specific User in an Init Script
Systemd: "Environment" Directive to Set Path
How to Identify Multiple Usb-Serial Adapters Under Ubuntu 10.1
Searching Multiple Patterns (Words) with Ack
Finding Original MAC Address from Hardware Itself
Stack Resident Buffer Overflow on 64-Bit
Linux, Print All Lines in a File, Not Starting With
Cannot Install Extensions in Visual Studio Code
How to Trace Per-File Io Operations in Linux
How to Catch Duplicate Entries in Text File in Linux
Determine the Os Version, Linux and Windows from Powershell
How to Know Whether a Copy-On-Write Page Is an Actual Copy
"Current" in Linux Kernel Code
How to Force Node.Js Require to Be Case Sensitive