From a3a29795087afe0cef657ee86782e81638c46ee1 Mon Sep 17 00:00:00 2001 From: JustAnotherArchivist Date: Wed, 7 Jul 2021 03:35:01 +0000 Subject: [PATCH] Add combine-by-prefix --- combine-by-prefix | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100755 combine-by-prefix diff --git a/combine-by-prefix b/combine-by-prefix new file mode 100755 index 0000000..982e6c6 --- /dev/null +++ b/combine-by-prefix @@ -0,0 +1,6 @@ +#!/bin/bash +# Usage: pass lines into stdin; consecutive lines with the same first field (everything up to the first whitespace) will be grouped together on stdout. +# Input lines: 'A 1', 'A 2', 'B 3', 'C 4', 'C 5', 'B 6' +# Output lines: 'A 1 2', 'B 3', 'C 4 5', 'B 6' +# Whitespace within input lines beyond the first whitespace after the prefix is preserved. A single space is used to separate prefix and combined lines. +exec awk '($1 != lastPrefix) || (NR == 1) { if (NR != 1) { printf "\n"; } printf "%s", $1; lastPrefix = $1; } { printf " %s", substr($0, index($0, FS) + 1); } END { printf "\n"; }'