Delete all lines from middle of a line matching a string until the second string match is found

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
I am trying to remove the error messages printed into my file.
I have this:
addr:1122c:1234:
addr:11230:5678:
addr:11223:01Error:abcdef(x, y) = z, value = a
Error:hijklm(v, q) = w, value = b
Error:nopqrst(x, y) = z, value = d
Error:uvwxyz(l, m) = z, value = e
Error:1234(u, t) = z, value = f
Error:567(r, s) = z, value = g
err_total = 9846, err_sub = 0, err_mask = 239
1 Duration: xyz, abc
0 Duration: pqr, def
23:
addr:11238:4567:
addr:1123c:8901:
I need to remove all the error messages upto the next addr appears.
Required output is:
addr:1122c:1234:
addr:11230:5678:
addr:11223:0123:
addr:11238:4567:
addr:1123c:8901:
I have tried:
sed -i "/bErrorb/d" file_name
But this removes the lines starting from Error and did not remove the line where Error string started from the middle.
I am new to regular expressions, an explaination would be really helpful.
Edit:
I am using sed -i '/Error/,/addr/d' filename
but this removes the whole line and does not give what I am looking for.
text-processing sed awk
add a comment |Â
up vote
3
down vote
favorite
I am trying to remove the error messages printed into my file.
I have this:
addr:1122c:1234:
addr:11230:5678:
addr:11223:01Error:abcdef(x, y) = z, value = a
Error:hijklm(v, q) = w, value = b
Error:nopqrst(x, y) = z, value = d
Error:uvwxyz(l, m) = z, value = e
Error:1234(u, t) = z, value = f
Error:567(r, s) = z, value = g
err_total = 9846, err_sub = 0, err_mask = 239
1 Duration: xyz, abc
0 Duration: pqr, def
23:
addr:11238:4567:
addr:1123c:8901:
I need to remove all the error messages upto the next addr appears.
Required output is:
addr:1122c:1234:
addr:11230:5678:
addr:11223:0123:
addr:11238:4567:
addr:1123c:8901:
I have tried:
sed -i "/bErrorb/d" file_name
But this removes the lines starting from Error and did not remove the line where Error string started from the middle.
I am new to regular expressions, an explaination would be really helpful.
Edit:
I am using sed -i '/Error/,/addr/d' filename
but this removes the whole line and does not give what I am looking for.
text-processing sed awk
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I am trying to remove the error messages printed into my file.
I have this:
addr:1122c:1234:
addr:11230:5678:
addr:11223:01Error:abcdef(x, y) = z, value = a
Error:hijklm(v, q) = w, value = b
Error:nopqrst(x, y) = z, value = d
Error:uvwxyz(l, m) = z, value = e
Error:1234(u, t) = z, value = f
Error:567(r, s) = z, value = g
err_total = 9846, err_sub = 0, err_mask = 239
1 Duration: xyz, abc
0 Duration: pqr, def
23:
addr:11238:4567:
addr:1123c:8901:
I need to remove all the error messages upto the next addr appears.
Required output is:
addr:1122c:1234:
addr:11230:5678:
addr:11223:0123:
addr:11238:4567:
addr:1123c:8901:
I have tried:
sed -i "/bErrorb/d" file_name
But this removes the lines starting from Error and did not remove the line where Error string started from the middle.
I am new to regular expressions, an explaination would be really helpful.
Edit:
I am using sed -i '/Error/,/addr/d' filename
but this removes the whole line and does not give what I am looking for.
text-processing sed awk
I am trying to remove the error messages printed into my file.
I have this:
addr:1122c:1234:
addr:11230:5678:
addr:11223:01Error:abcdef(x, y) = z, value = a
Error:hijklm(v, q) = w, value = b
Error:nopqrst(x, y) = z, value = d
Error:uvwxyz(l, m) = z, value = e
Error:1234(u, t) = z, value = f
Error:567(r, s) = z, value = g
err_total = 9846, err_sub = 0, err_mask = 239
1 Duration: xyz, abc
0 Duration: pqr, def
23:
addr:11238:4567:
addr:1123c:8901:
I need to remove all the error messages upto the next addr appears.
Required output is:
addr:1122c:1234:
addr:11230:5678:
addr:11223:0123:
addr:11238:4567:
addr:1123c:8901:
I have tried:
sed -i "/bErrorb/d" file_name
But this removes the lines starting from Error and did not remove the line where Error string started from the middle.
I am new to regular expressions, an explaination would be really helpful.
Edit:
I am using sed -i '/Error/,/addr/d' filename
but this removes the whole line and does not give what I am looking for.
text-processing sed awk
edited 10 hours ago
asked 2 days ago
lost_wanderer
185
185
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
sed is not really good in Multiline Matching.
You can trick it to do what you want, but then imo perl is easier to handle.
Try this:
perl -pe 'BEGINundef $/;; s/Error.*?(^[0-9]* Duration: [^n]*n)+//smg;'
Explanation:
BEGIN do_something;: Do something once at the beginningundef $/: Ignore line endingss///SubstituteError.*Match any string beginning with "Error".?Make the previous match ungreedy, for that it stops at the following match or in this case matching group...()+Make a matching group, that needs to be matched at least once (+).^[0-9]* Duration: [^n]*n: Match the whole line with Duration including.
(via)
Thank you for the answer, works like a charm for the example i gave. However, I had a different example I was working on. I have updated the question, can u please take a look and suggest changes?
â lost_wanderer
2 days ago
1
Updated the answer.
â RoVo
2 days ago
add a comment |Â
up vote
2
down vote
Rather than deleting the "Error:" lines, why not extract just the lines you want with:
grep -E '^ addr:' file_name | sed -e 's/Error:.*//'
I have other lines in the file that does not begin with 'addr' but are needed. Hence I asked for way to remove the unwanted lines.
â lost_wanderer
2 days ago
2
I refuse to track a problem that's revealed a tiny bit at a time. You keep changing requirements, and that's unacceptable.
â waltinator
2 days ago
Sorry for the miscommunication, my intention was just to remove the unwanted lines and I guess I dint convey it in the best way. Nevertheless, thank you for your answer, upvoted.
â lost_wanderer
yesterday
add a comment |Â
up vote
1
down vote
This will generate the output you have been looking for:
$ cat file_name | grep -v
-e '^Error:'
-e '^err_total'
-e '^.*[0-9] Duration:' |
sed ':a;$!N;s/Error:.*n(.*[0-9]):/1:/;ta;P;D'
Frist remove all the Error, err_total, and 12345 Duration: stuff. Then search for the Error: ... interrupting your output, remove the newline(n), search for the next occurence of a number (.*[0-9]:) an append it to the current line.
Thank you for the answer, works like a charm for the example i gave. However, I had a different example I was working on. I have updated the question, can u please take a look and suggest changes?
â lost_wanderer
2 days ago
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
sed is not really good in Multiline Matching.
You can trick it to do what you want, but then imo perl is easier to handle.
Try this:
perl -pe 'BEGINundef $/;; s/Error.*?(^[0-9]* Duration: [^n]*n)+//smg;'
Explanation:
BEGIN do_something;: Do something once at the beginningundef $/: Ignore line endingss///SubstituteError.*Match any string beginning with "Error".?Make the previous match ungreedy, for that it stops at the following match or in this case matching group...()+Make a matching group, that needs to be matched at least once (+).^[0-9]* Duration: [^n]*n: Match the whole line with Duration including.
(via)
Thank you for the answer, works like a charm for the example i gave. However, I had a different example I was working on. I have updated the question, can u please take a look and suggest changes?
â lost_wanderer
2 days ago
1
Updated the answer.
â RoVo
2 days ago
add a comment |Â
up vote
4
down vote
accepted
sed is not really good in Multiline Matching.
You can trick it to do what you want, but then imo perl is easier to handle.
Try this:
perl -pe 'BEGINundef $/;; s/Error.*?(^[0-9]* Duration: [^n]*n)+//smg;'
Explanation:
BEGIN do_something;: Do something once at the beginningundef $/: Ignore line endingss///SubstituteError.*Match any string beginning with "Error".?Make the previous match ungreedy, for that it stops at the following match or in this case matching group...()+Make a matching group, that needs to be matched at least once (+).^[0-9]* Duration: [^n]*n: Match the whole line with Duration including.
(via)
Thank you for the answer, works like a charm for the example i gave. However, I had a different example I was working on. I have updated the question, can u please take a look and suggest changes?
â lost_wanderer
2 days ago
1
Updated the answer.
â RoVo
2 days ago
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
sed is not really good in Multiline Matching.
You can trick it to do what you want, but then imo perl is easier to handle.
Try this:
perl -pe 'BEGINundef $/;; s/Error.*?(^[0-9]* Duration: [^n]*n)+//smg;'
Explanation:
BEGIN do_something;: Do something once at the beginningundef $/: Ignore line endingss///SubstituteError.*Match any string beginning with "Error".?Make the previous match ungreedy, for that it stops at the following match or in this case matching group...()+Make a matching group, that needs to be matched at least once (+).^[0-9]* Duration: [^n]*n: Match the whole line with Duration including.
(via)
sed is not really good in Multiline Matching.
You can trick it to do what you want, but then imo perl is easier to handle.
Try this:
perl -pe 'BEGINundef $/;; s/Error.*?(^[0-9]* Duration: [^n]*n)+//smg;'
Explanation:
BEGIN do_something;: Do something once at the beginningundef $/: Ignore line endingss///SubstituteError.*Match any string beginning with "Error".?Make the previous match ungreedy, for that it stops at the following match or in this case matching group...()+Make a matching group, that needs to be matched at least once (+).^[0-9]* Duration: [^n]*n: Match the whole line with Duration including.
(via)
edited 2 days ago
answered 2 days ago
RoVo
4,516932
4,516932
Thank you for the answer, works like a charm for the example i gave. However, I had a different example I was working on. I have updated the question, can u please take a look and suggest changes?
â lost_wanderer
2 days ago
1
Updated the answer.
â RoVo
2 days ago
add a comment |Â
Thank you for the answer, works like a charm for the example i gave. However, I had a different example I was working on. I have updated the question, can u please take a look and suggest changes?
â lost_wanderer
2 days ago
1
Updated the answer.
â RoVo
2 days ago
Thank you for the answer, works like a charm for the example i gave. However, I had a different example I was working on. I have updated the question, can u please take a look and suggest changes?
â lost_wanderer
2 days ago
Thank you for the answer, works like a charm for the example i gave. However, I had a different example I was working on. I have updated the question, can u please take a look and suggest changes?
â lost_wanderer
2 days ago
1
1
Updated the answer.
â RoVo
2 days ago
Updated the answer.
â RoVo
2 days ago
add a comment |Â
up vote
2
down vote
Rather than deleting the "Error:" lines, why not extract just the lines you want with:
grep -E '^ addr:' file_name | sed -e 's/Error:.*//'
I have other lines in the file that does not begin with 'addr' but are needed. Hence I asked for way to remove the unwanted lines.
â lost_wanderer
2 days ago
2
I refuse to track a problem that's revealed a tiny bit at a time. You keep changing requirements, and that's unacceptable.
â waltinator
2 days ago
Sorry for the miscommunication, my intention was just to remove the unwanted lines and I guess I dint convey it in the best way. Nevertheless, thank you for your answer, upvoted.
â lost_wanderer
yesterday
add a comment |Â
up vote
2
down vote
Rather than deleting the "Error:" lines, why not extract just the lines you want with:
grep -E '^ addr:' file_name | sed -e 's/Error:.*//'
I have other lines in the file that does not begin with 'addr' but are needed. Hence I asked for way to remove the unwanted lines.
â lost_wanderer
2 days ago
2
I refuse to track a problem that's revealed a tiny bit at a time. You keep changing requirements, and that's unacceptable.
â waltinator
2 days ago
Sorry for the miscommunication, my intention was just to remove the unwanted lines and I guess I dint convey it in the best way. Nevertheless, thank you for your answer, upvoted.
â lost_wanderer
yesterday
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Rather than deleting the "Error:" lines, why not extract just the lines you want with:
grep -E '^ addr:' file_name | sed -e 's/Error:.*//'
Rather than deleting the "Error:" lines, why not extract just the lines you want with:
grep -E '^ addr:' file_name | sed -e 's/Error:.*//'
answered 2 days ago
waltinator
19.9k73868
19.9k73868
I have other lines in the file that does not begin with 'addr' but are needed. Hence I asked for way to remove the unwanted lines.
â lost_wanderer
2 days ago
2
I refuse to track a problem that's revealed a tiny bit at a time. You keep changing requirements, and that's unacceptable.
â waltinator
2 days ago
Sorry for the miscommunication, my intention was just to remove the unwanted lines and I guess I dint convey it in the best way. Nevertheless, thank you for your answer, upvoted.
â lost_wanderer
yesterday
add a comment |Â
I have other lines in the file that does not begin with 'addr' but are needed. Hence I asked for way to remove the unwanted lines.
â lost_wanderer
2 days ago
2
I refuse to track a problem that's revealed a tiny bit at a time. You keep changing requirements, and that's unacceptable.
â waltinator
2 days ago
Sorry for the miscommunication, my intention was just to remove the unwanted lines and I guess I dint convey it in the best way. Nevertheless, thank you for your answer, upvoted.
â lost_wanderer
yesterday
I have other lines in the file that does not begin with 'addr' but are needed. Hence I asked for way to remove the unwanted lines.
â lost_wanderer
2 days ago
I have other lines in the file that does not begin with 'addr' but are needed. Hence I asked for way to remove the unwanted lines.
â lost_wanderer
2 days ago
2
2
I refuse to track a problem that's revealed a tiny bit at a time. You keep changing requirements, and that's unacceptable.
â waltinator
2 days ago
I refuse to track a problem that's revealed a tiny bit at a time. You keep changing requirements, and that's unacceptable.
â waltinator
2 days ago
Sorry for the miscommunication, my intention was just to remove the unwanted lines and I guess I dint convey it in the best way. Nevertheless, thank you for your answer, upvoted.
â lost_wanderer
yesterday
Sorry for the miscommunication, my intention was just to remove the unwanted lines and I guess I dint convey it in the best way. Nevertheless, thank you for your answer, upvoted.
â lost_wanderer
yesterday
add a comment |Â
up vote
1
down vote
This will generate the output you have been looking for:
$ cat file_name | grep -v
-e '^Error:'
-e '^err_total'
-e '^.*[0-9] Duration:' |
sed ':a;$!N;s/Error:.*n(.*[0-9]):/1:/;ta;P;D'
Frist remove all the Error, err_total, and 12345 Duration: stuff. Then search for the Error: ... interrupting your output, remove the newline(n), search for the next occurence of a number (.*[0-9]:) an append it to the current line.
Thank you for the answer, works like a charm for the example i gave. However, I had a different example I was working on. I have updated the question, can u please take a look and suggest changes?
â lost_wanderer
2 days ago
add a comment |Â
up vote
1
down vote
This will generate the output you have been looking for:
$ cat file_name | grep -v
-e '^Error:'
-e '^err_total'
-e '^.*[0-9] Duration:' |
sed ':a;$!N;s/Error:.*n(.*[0-9]):/1:/;ta;P;D'
Frist remove all the Error, err_total, and 12345 Duration: stuff. Then search for the Error: ... interrupting your output, remove the newline(n), search for the next occurence of a number (.*[0-9]:) an append it to the current line.
Thank you for the answer, works like a charm for the example i gave. However, I had a different example I was working on. I have updated the question, can u please take a look and suggest changes?
â lost_wanderer
2 days ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
This will generate the output you have been looking for:
$ cat file_name | grep -v
-e '^Error:'
-e '^err_total'
-e '^.*[0-9] Duration:' |
sed ':a;$!N;s/Error:.*n(.*[0-9]):/1:/;ta;P;D'
Frist remove all the Error, err_total, and 12345 Duration: stuff. Then search for the Error: ... interrupting your output, remove the newline(n), search for the next occurence of a number (.*[0-9]:) an append it to the current line.
This will generate the output you have been looking for:
$ cat file_name | grep -v
-e '^Error:'
-e '^err_total'
-e '^.*[0-9] Duration:' |
sed ':a;$!N;s/Error:.*n(.*[0-9]):/1:/;ta;P;D'
Frist remove all the Error, err_total, and 12345 Duration: stuff. Then search for the Error: ... interrupting your output, remove the newline(n), search for the next occurence of a number (.*[0-9]:) an append it to the current line.
edited 2 days ago
answered 2 days ago
Simon Sudler
893111
893111
Thank you for the answer, works like a charm for the example i gave. However, I had a different example I was working on. I have updated the question, can u please take a look and suggest changes?
â lost_wanderer
2 days ago
add a comment |Â
Thank you for the answer, works like a charm for the example i gave. However, I had a different example I was working on. I have updated the question, can u please take a look and suggest changes?
â lost_wanderer
2 days ago
Thank you for the answer, works like a charm for the example i gave. However, I had a different example I was working on. I have updated the question, can u please take a look and suggest changes?
â lost_wanderer
2 days ago
Thank you for the answer, works like a charm for the example i gave. However, I had a different example I was working on. I have updated the question, can u please take a look and suggest changes?
â lost_wanderer
2 days ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1062808%2fdelete-all-lines-from-middle-of-a-line-matching-a-string-until-the-second-string%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password