Skip to content

Instantly share code, notes, and snippets.

@rf5860
Last active October 10, 2019 23:03
Show Gist options
  • Save rf5860/6085c86cd4a99b032a7e079904de3109 to your computer and use it in GitHub Desktop.
Save rf5860/6085c86cd4a99b032a7e079904de3109 to your computer and use it in GitHub Desktop.
Complex Sed Example.sed
/* n flag - Turn off printing by default */
1{h; /* On the first line, save the line to temporary storage ('hold' buffer) */
d}; /* Then delete the current line and go to the next */
2{H; /* On the second line, append the current line to the 'hold' buffer */
x; /* Swap the line and the 'hold' buffer */
s:\n::; /* Remove newlines from the line */
x; /* Swap it with the 'hold' buffer */
d}; /* Delete the current line */
3{s:\(.*\): (\1):; /* Add a space to the end of the line */
H}; /* Append this line to the 'hold' buffer */
\%/usr/local% {s:\(.*\): [\1]:; /* On lines matching '\/usr\/local\/, Wrap in square brackets, and prepend a space */
H; /* Append this line to the 'hold' buffer */
x; /* Swap it back into the 'line' buffer */
s:\n::g; /* Remove all new lines */
p}; /* Print it */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment