#!/bin/sh # The MIT License (MIT) # # Copyright (c) 2026 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # chopleads [options...] [files...] # # Ignore the longest common leading parts of all non-empty lines. A typical # use case is when dealing with lines starting with full filenames, where # all files come from the same subfolder, as getting rid of the long common # starts can vastly reduce the clutter. # # The options are, available both in single and double-dash versions # # -h, -help show this help message buf=0 while [ $# -gt 0 ]; do case "$1" in -b|--b|-buffered|--buffered) buf=1; shift; continue ;; -h|--h|-help|--help) awk '/^# +chopleads /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; -) break ;; --) shift; break ;; -*) printf "unsupported option '%s'\n" "$1" >&2 exit 1 ;; esac break done # show all non-existing files given failed=0 for arg in "$@"; do [ "${arg}" = "-" ] && continue [ -e "${arg}" ] && continue printf "no file named \"%s\"\n" "${arg}" >&2 failed=1 done [ "${failed}" -gt 0 ] && exit 2 flush=0 if [ "${buf}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then flush=1 fi awk -v flush="${flush}" ' BEGIN { leadlen = -1 } function dump() { for (i = 1; i <= kept; i++) { print lines[i] delete lines[i] if (flush) fflush() } # prevent output of many empty lines at the end kept = 0 } { if (leadlen == 0) { dump() print if (flush) fflush() next } lines[++kept] = $0 } { if ($0 == "") next # the full first non-empty line becomes the temporary lead if (leadlen < 0) { lead = $0 first = substr(lead, 1, 1) leadlen = length next } # avoid loops if very start of a line implies no common lead if (first != substr($0, 1, 1)) { lead = "" leadlen = 0 dump() next } # common lead never exceeds the shortest non-empty line l = length if (leadlen > l) { leadlen = l lead = substr(lead, 1, leadlen) } while (leadlen > 0) { s = substr($0, 1, leadlen) if (s == lead) { lead = s break } leadlen-- lead = substr(lead, 1, leadlen) } # if no leads are common, show all previous lines right away if (leadlen == 0) dump() } END { # with only 1 line of input, avoid emitting a single empty line # for sure skip = (NR == 1) ? 0 : leadlen + 1 for (i = 1; i <= kept; i++) { line = lines[i] if ((skip > 0) && (line != "")) line = substr(line, skip) print line } } ' "$@"