Home > database >  Remove linse before and after giving string (sed?)
Remove linse before and after giving string (sed?)

Time:12-15

I'm tring to parsing a file in Ubuntu. The file looks like that:

---
13:21:11_09/11/22: Sync between <Repo Name> to <Other Repo Name>

Everything up-to-date
---
13:21:11_09/11/22:  Sync between <Repo Name> to <Other Repo Name>

Everything up-to-date
---
13:21:11_09/11/22: Sync between  Sync between <Repo Name> to <Other Repo Name>
remote: warning: File
<File Name>.zip is 70.04 MB; this is larger than GitHub's 
recommended maximum file size of 50.00 MB

remote: error: Trace: 00000000000000000000
remote: error: See http://git.io/iEPt8g for more information.
remote: error: File 
<File Name>.zip 
is 135.74 MB; this exceeds GitHub's file size limit of 100.00 MB
---
13:21:11_09/11/22: Sync between  Sync between <Repo Name> to <Other Repo Name>

Everything up-to-date
    ---
13:21:11_09/11/22: Sync between  Sync between <Repo Name> to <Other Repo Name>
Upload <Repo Name>

I tring to remove all the sections that not contain an error.

Something like this:

---
13:21:11_09/11/22: Sync between  Sync between <Repo Name> to <Other Repo Name>
remote: warning: File
<File Name>.zip is 70.04 MB; this is larger than GitHub's 
recommended maximum file size of 50.00 MB

remote: error: Trace: 00000000000000000000
remote: error: See http://git.io/iEPt8g for more information.
remote: error: File 
<File Name>.zip 
is 135.74 MB; this exceeds GitHub's file size limit of 100.00 MB

Any suggestions?

Thanks for your help.

CodePudding user response:

I would harness GNU AWK for this task following way, let file.txt content be

---
13:21:11_09/11/22: Sync between <Repo Name> to <Other Repo Name>

Everything up-to-date
---
13:21:11_09/11/22:  Sync between <Repo Name> to <Other Repo Name>

Everything up-to-date
---
13:21:11_09/11/22: Sync between  Sync between <Repo Name> to <Other Repo Name>
remote: warning: File
<File Name>.zip is 70.04 MB; this is larger than GitHub's 
recommended maximum file size of 50.00 MB

remote: error: Trace: 00000000000000000000
remote: error: See http://git.io/iEPt8g for more information.
remote: error: File 
<File Name>.zip 
is 135.74 MB; this exceeds GitHub's file size limit of 100.00 MB
---
13:21:11_09/11/22: Sync between  Sync between <Repo Name> to <Other Repo Name>

Everything up-to-date
    ---
13:21:11_09/11/22: Sync between  Sync between <Repo Name> to <Other Repo Name>
Upload <Repo Name>

then

awk 'BEGIN{RS="---\n"}/error/{print "---\n" $0}' file.txt

gives output

---
13:21:11_09/11/22: Sync between  Sync between <Repo Name> to <Other Repo Name>
remote: warning: File
<File Name>.zip is 70.04 MB; this is larger than GitHub's 
recommended maximum file size of 50.00 MB

remote: error: Trace: 00000000000000000000
remote: error: See http://git.io/iEPt8g for more information.
remote: error: File 
<File Name>.zip 
is 135.74 MB; this exceeds GitHub's file size limit of 100.00 MB

Explanation: I inform GNU AWK that row separator (RS) is triple dash followed by newline, then for rows which contain error somewhere I print triple dash newline followed by content of row ($0).

(tested in GNU Awk 5.0.1)

  • Related