skip to Main Content

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.

2

Answers


  1. 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)

    Login or Signup to reply.
  2. This might work for you (GNU sed):

    sed -En '/^---$/!{H;$!d};x;/error/p;x;h' file
    

    Turn on extended regexps and off implicit printing.

    If the current line does not contain only ---, append it to the hold space (an alternative register to the current register known as the pattern space) and then delete the pattern space if it is not the last line.

    Otherwise, swap to the hold space and if it contains the word error, print it. Then return to the pattern space and replace the hold space by the current line.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search