Delete match line and required lines

sed delete multiple lines matching pattern
sed delete line before match
sed delete line matching pattern
sed delete line after match
sed delete lines matching multiple patterns
vim delete lines matching
sed remove lines starting with pattern
grep remove lines after match

i have a text files like

DATA SENT TO : 1
DATA SENT TO : 2
DATA SENT TO : 3
DATA SENT TO : 4
DATA SENT TO : 5
Failed to sent data 
DATA SENT TO : 6
Failed to sent data   
DATA SENT TO : 7
Failed to sent data  
DATA SENT TO : 8
DATA SENT TO : 9
  % SENT    % FAILED    AVG   Time    Time   Time    Current
                       Dload  Upload   Total   Spent    Left  
  0            0           0        0      0      0         0
DATA SENT TO : 10
  % SENT    % FAILED    AVG   Time    Time     Time  Current
                        Dload  Upload   Total   Spent    Left  
  0            0           0        0      0      0         0
DATA SENT TO : 11
  % SENT    % FAILED    AVG   Time    Time     Time  Current
                        Dload  Upload   Total   Spent    Left  
  0            0           0        0      0      0         0
DATA SENT TO : 12
  % SENT    % FAILED    AVG   Time    Time     Time  Current
                        Dload  Upload   Total   Spent    Left  
  0            0           0        0      0      0         0
DATA SENT TO : 13
Failed to sent 
DATA SENT TO : 14
DATA SENT TO : 15  

form the text file i need find out total count of data sent. if i tried to use: wc -l file.txt then it will give total line count. but it is not accurate count As i need output as

DATA SENT TO : 1
DATA SENT TO : 2
DATA SENT TO : 3
DATA SENT TO : 4
DATA SENT TO : 5
DATA SENT TO : 9
DATA SENT TO : 10
DATA SENT TO : 11
DATA SENT TO : 12
DATA SENT TO : 13
DATA SENT TO : 15  

if used the commnad sed -e /Failed/ {N; d;} --> it will delete the matched and next line but iam unable to delete the following 3 lines

% SENT    % FAILED    AVG   Time    Time     Time  Current
                       Dload  Upload   Total   Spent    Left  
  0            0           0        0      0      0         0

It's not clear if you want the lines output or a count of the lines output or both so:

$ awk '/DATA SENT TO/ && !f{print} {f=/Failed to sent/}' file
DATA SENT TO : 1
DATA SENT TO : 2
DATA SENT TO : 3
DATA SENT TO : 4
DATA SENT TO : 5
DATA SENT TO : 9
DATA SENT TO : 10
DATA SENT TO : 11
DATA SENT TO : 12
DATA SENT TO : 13
DATA SENT TO : 15

$ awk '/DATA SENT TO/ && !f{cnt++} {f=/Failed to sent/} END{print cnt+0}' file
11

$ awk '/DATA SENT TO/ && !f{print;cnt++} {f=/Failed to sent/} END{print "Total=" cnt+0}' file
DATA SENT TO : 1
DATA SENT TO : 2
DATA SENT TO : 3
DATA SENT TO : 4
DATA SENT TO : 5
DATA SENT TO : 9
DATA SENT TO : 10
DATA SENT TO : 11
DATA SENT TO : 12
DATA SENT TO : 13
DATA SENT TO : 15
Total=11

About Deleting Match Lines | Civil 3D 2017, You can delete match line labels. Selecting any match line label automatically selects all match line labels in that view frame group. To delete them individually, select them by using Ctrl + click, and then press the Delete key. Suppress implicit printing. Delete lines beginning Failed to sent and the following line. Only print lines that begin DATA. To obtain the number of such lines use: sed -n '/^Failed to sent/N;/^DATA/p' file | wc -l Or: sed -n '/^Failed to sent/N;/^DATA/p' file | sed -n '$='


Use awk to sum the values and substract the failing lines:

awk '/DATA SENT TO/{sum++} /Failed to sent/{sum--} END{print sum}' test.txt

Remove line containing certain string and the following line, So I want to find an entry matching the pattern and remove it as well as the next line. Hence I don't need to handle consecutive match lines, but thanks anyway  Delete all lines containing a pattern. The ex command g is very useful for acting on lines that match a pattern. You can use it with the d command, to delete all lines that contain a particular pattern, or all lines that do not contain a pattern.


This might work for you (GNU sed):

sed -n '/^Failed to sent/N;/^DATA/p' file

Suppress implicit printing. Delete lines beginning Failed to sent and the following line. Only print lines that begin DATA.

To obtain the number of such lines use:

sed -n '/^Failed to sent/N;/^DATA/p' file | wc -l

Or:

sed -n '/^Failed to sent/N;/^DATA/p' file | sed -n '$='

Deleting a range of n lines before and after a matched line?, You're almost there; you can also specify a range of lines, which is in the form of [​line1],[line2][command] , instead of just [line][command] (like you're using now). Remove a line from the file if it is found – All the instances; Insert before a matching line using insertbefore parameter; Insert after the matching line using insertafter parameter; validate the changes are correct before saving . Example 1: Validate if a line is present in the file without any modification


Delete all lines containing a pattern | Vim Tips Wiki, The ex command g is very useful for acting on lines that match a pattern. You can use it with Can we delete/not delete the line that precedes the search string? First, the in-line modifier (?-s) ensures you that the dot meta-character will match standard characters, only, even if you previously checked the . matches newline option ! Then, the regex engine looks, from the beginning of a line ( ^ ), for any amount, even empty , of characters ( .* ), followed by what you’re searching, then followed by


Delete Lines Matching a Specific Pattern in a File using SED , Just like in VIM, we will be using the d command to delete specific pattern space with SED. To begin with, if you want to delete a line containing  Use the following sed command to remove all the lines from the file, except the specified range of line. # sed '3,6!d' sed-demo.txt After deletion: 3 RHEL 4 Red Hat 5 Fedora 6 Arch Linux. 6) How to Delete Empty or Blank Lines from a File? The following sed command will remove the empty or blank lines from sed-demo.txt file.


Delete Lines Matching Specific Pattern in a File using VIM, Linux system admins work with files day in day out. At some point, you may want just to delete lines matching a specific pattern or a string in a file. ALL is required to emphasize that NEXT is not the default. X Restricts the lines deleted to those that are excluded. NX Restricts the lines deleted to those that are not excluded. labela, labelb Labels identifying the start and end of the group of lines which are deleted, including the lines with the labels. To delete one line, enter the same label twice.