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
I would harness GNU
AWK
for this task following way, letfile.txt
content bethen
gives output
Explanation: I inform GNU
AWK
that row separator (RS
) is triple dash followed by newline, then for rows which containerror
somewhere Iprint
triple dash newline followed by content of row ($0
).(tested in GNU Awk 5.0.1)
This might work for you (GNU sed):
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.