How to delete from a text file, all lines that contain a specific string?
To remove the line and print the output to standard out:
sed '/pattern to match/d' ./infile
To directly modify the file – does not work with BSD sed:
sed -i '/pattern to match/d' ./infile
Same, but for BSD sed (Mac OS X and FreeBSD) – does not work with GNU sed:
sed -i '' '/pattern to match/d' ./infile
To directly modify the file (and create a backup) – works with BSD and GNU sed:
sed -i.bak '/pattern to match/d' ./infile
sed or awk: delete n lines following a pattern
I'll have a go at this.
To delete 5 lines after a pattern (including the line with the pattern):
sed -e '/pattern/,+5d' file.txt
To delete 5 lines after a pattern (excluding the line with the pattern):
sed -e '/pattern/{n;N;N;N;N;d}' file.txt
Delete to end of line after a match, keep lines not matched
You need to leave out the -n
option.
Check out http://www.gnu.org/software/sed/manual/sed.html#index-g_t_002d_002dquiet-7
Use sed to delete all lines starting with pattern b after line with pattern a
With GNU sed
, you may use
sed '/DELETE ME/{:a;N;s/\n[[:blank:]]*-.*//;ta;!P;D}' file
See the online sed
demo:
s='first line
second line DELETE ME
- third line
- fourth line
fifth line
sixth line DELETE ME
seventh line
- eighth line'
sed '/DELETE ME/{:a;N;s/\n[[:blank:]]*-.*//;ta;!P;D}' <<< "$s"
Output:
first line
fifth line
seventh line
- eighth line
Details
/DELETE ME/
- finds all lines that containDELETE ME
string{:a;N;s/\n[[:blank:]]*-.*//;ta;!P;D}
- if the line matchingDELETE ME
is found, this block is entered::a
- ana
label marks the current positionN
- reads the next line with\n
at the start into the pattern spaces/\n[[:blank:]]*-.*//
- finds and removes the newline, 0+ blank chars,-
and the rest of the stringta
- if the substitution occurred,sed
goes to the position marked witha
!P
- otherwise, prints the pattern space content until the first newline (i.e. prints the first line)D
- deletes the pattern space content until the first new line, i.e. deletes the first line inside pattern space, and restarts cycle with the resultant pattern space, without reading a new line of input.
How to delete the line that matches a pattern and the line after it with sed?
Using an extension of the GNU
version of sed:
sed -e '/FLAG/,+1 d' infile
It yields:
good text
good text
good text
good text
good test
good text
SED to remove a Line with REGEX Pattern
$ sed '/^A.*lorem$/d' file.txt
^A
: starts with anA
.*
: stuff in the middlelorem$
: ends withlorem
sed - Remove previous line and current line based on pattern
This might work for you (GNU sed):
sed 'N;/\n.*999/d;P;D' file
Open a running window of two lines throughout the length of the file.
If the second line of the window contains 999
delete both lines.
Otherwise, print the first line of the window, delete the first line and repeat.
An alternative solution for line 1 or 2 or more contiguous lines containing 999
:
sed -n ':a;$!N;/\n.*999/{:b;n;/999/bb;ba};/999/!P;D' file
Delete line that matches pattern and all subsequent lines until second pattern
You may use
sed '/delete,/,/deletion\./d' file > outfile
Here,
/delete,/,/deletion\./
tellssed
to match a portion of text between (and including) the lines, starting with one contrainingdelete,
and ending with the line havingdeletion.
(note.
must be escaped to match a literal dot)d
tellssed
to remove that block of lines.
See an online sed
demo.
Related Topics
Using Winscp to Grab a File Through a Tunnel
How to Make Linux Ignore a Keyboard While Keeping It Available for My Program to Read
Where Is User's Cron Job Stored After "Crontab -E"
Do Here-Strings Undergo Word-Splitting
Linux: How to Check The Largest Contiguous Address Range Available to a Process
How Event Packet Header Is Getiing in Hci_Send_Req API Implementation
Starting Multiple Firefox Profiles with Batch File in Linux
User Time Larger Than Real Time
How to Find List of Valid Locales in My Linux Using Perl
Copying Files with Wildcard (*) to a Folder in a Bash Script - Why Isn't It Working
How to Generic .Htaccess to Prevent Hotlink
Releasing All Keys After Disabling The Keyboard in X11/Linux Using Xinput
Copying an Image to The Clipboard from Command Line
Vagrant - How to Mount The Virtualbox Shared Folder? ("Vboxsf" Not Available)