ram kumar
ram kumar

Reputation: 29

Delete match line and required lines

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

Upvotes: 0

Views: 72

Answers (3)

potong
potong

Reputation: 58361

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 '$='

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203169

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

Upvotes: 1

steffen
steffen

Reputation: 16938

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

Upvotes: 0

Related Questions