Skip to content

Instantly share code, notes, and snippets.

@x-yuri

x-yuri/1.diff Secret

Last active March 31, 2019 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x-yuri/e3c366893ba8b6c07385f6822a3ba904 to your computer and use it in GitHub Desktop.
Save x-yuri/e3c366893ba8b6c07385f6822a3ba904 to your computer and use it in GitHub Desktop.
--- /home/yuri/_/1.sh 2019-03-31 17:56:18.380352272 +0300
+++ /home/yuri/_/2.sh 2019-03-31 17:56:05.463685315 +0300
@@ -13,11 +13,11 @@
3
]
...'
-sed -ne ' ;# "-n" suppresses autoprint of pattern space
- /^\[/!d ;# skip lines that donot begin a fresh block
- :a;$!N;/\n]/!ba ;# keep appending lines to pattern space until end of block
+sed -Ene ' ;# "-n" suppresses autoprint of pattern space
+ /^\[$/!d ;# skip lines that donot begin a fresh block
+ :a;N;/\n]$/!ba ;# keep appending lines to pattern space until end of block
G ;# append our makeshift counter (hold space) to pattern spc
- s/\n\{2\}$//p ;# only when counter had 2 chars in it, we print block
+ s/\n{2}$//p ;# only when counter had 2 chars in it, we print block
/\n$/!q ;# target block, 2nd block has been printed so quit
x;s/$/\n/;x ;# target block not reached, so increment
' <<<"$file"
#!/usr/bin/env bash
set -eu
file='...
[
1
]
...
[
2
]
...
[
3
]
...'
sed -Ene ' ;# "-n" suppresses autoprint of pattern space
/^\[$/!d ;# skip lines that donot begin a fresh block
:a;N;/\n]$/!ba ;# keep appending lines to pattern space until end of block
G ;# append our makeshift counter (hold space) to pattern spc
s/\n{2}$//p ;# only when counter had 2 chars in it, we print block
/\n$/!q ;# target block, 2nd block has been printed so quit
x;s/$/\n/;x ;# target block not reached, so increment
' <<<"$file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment