Tim Chase on Nostr: You might be familiar with sed '/pattern/G' for inserting a blank line *below* lines ...
You might be familiar with
sed '/pattern/G'
for inserting a blank line *below* lines matching /pattern/ but sometimes you want to put the blank line *above* the matching line. For that, you can use
sed '/pattern/{x;p;x;}'
It swaps the current space with the (empty) hold-space, prints that blank-line, then swaps them back so the matching line is there to be printed at the end of the cycle.
Published at
2024-12-20 14:06:53Event JSON
{
"id": "10a3d175983d794ec4bcadc6e313c7160e671e23a24da600d91ecede28e86cd8",
"pubkey": "9d1fe9f29c7a1e42464c3985f7185fe112b286140d32b8586dd34c6f92d6d9ee",
"created_at": 1734703613,
"kind": 1,
"tags": [
[
"content-warning",
"sed(1) trick for inserting a line above"
],
[
"proxy",
"https://mastodon.bsd.cafe/users/gumnos/statuses/113685536022806374",
"activitypub"
]
],
"content": "You might be familiar with\n\nsed '/pattern/G'\n\nfor inserting a blank line *below* lines matching /pattern/ but sometimes you want to put the blank line *above* the matching line. For that, you can use\n\nsed '/pattern/{x;p;x;}'\n\nIt swaps the current space with the (empty) hold-space, prints that blank-line, then swaps them back so the matching line is there to be printed at the end of the cycle.",
"sig": "c9e01072e3969727dfa49b6b0a314a2036bffd1af488d97975862fb8f24efc2c80ad8c1f9d5febd44986b4e2ed99cca10d707dfffbcbd32d7891c860dd3e4438"
}