|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# (C) DevConSoft 2018 |
| 4 | +# Author: Per Böhlin |
| 5 | +# |
| 6 | +# Source: https://github.com/devconsoft/slice |
| 7 | +# |
| 8 | + |
| 9 | +set -o errexit -o nounset -o pipefail |
| 10 | + |
| 11 | +# --- Usage and version --- |
| 12 | + |
| 13 | +usage() { |
| 14 | + cat <<EOF |
| 15 | +Usage: |
| 16 | + $SCRIPT -h|--help |
| 17 | + $SCRIPT --version |
| 18 | + $SCRIPT [-a|--text] [-n|--line-number] START_MARKER END_MARKER [FILE] |
| 19 | +
|
| 20 | +START_MARKER and END_MARKER is either a grep regular expression, or a line-number |
| 21 | +prefixed with dash(-). A standalone dash(-) indicates beginning/end of file. |
| 22 | +
|
| 23 | +if FILE is omitted, content is read from stdin. |
| 24 | +
|
| 25 | +Options: |
| 26 | + -h, --help output this small usage guide and exit |
| 27 | + --version output version information and exit |
| 28 | + -a, --text force text mode |
| 29 | + -n, --line-number print line number with output lines |
| 30 | + -B, --before-context=NUM print NUM lines of leading context |
| 31 | + -A, --after-context=NUM print NUM lines of trailing context |
| 32 | + -C, --context=NUM print NUM lines of output context |
| 33 | +
|
| 34 | +Examples: |
| 35 | + \$ $SCRIPT -2 foo file.txt |
| 36 | + Slice the file file.txt starting on line 2 ending with first occurrance |
| 37 | + after line 2 of a line matching 'foo' . |
| 38 | +
|
| 39 | + \$ expression | $SCRIPT -n -B 1 -A 2 a z |
| 40 | + Slice the input from stdin. First line shown will be 1 line before |
| 41 | + first line that matches 'a'; ending 2 lines after first |
| 42 | + occurance of 'z', and print line numbers. |
| 43 | +EOF |
| 44 | +} |
| 45 | + |
| 46 | +version() { |
| 47 | + echo "$SCRIPT -- version $VERSION" |
| 48 | + echo "Copyright DevConSoft, 2018" |
| 49 | +} |
| 50 | + |
| 51 | +# --- Globals and cleanup --- |
| 52 | + |
| 53 | +SCRIPT="$(basename "${BASH_SOURCE[0]}" )" |
| 54 | +VERSION=1.0 |
| 55 | +START_MARKER= |
| 56 | +END_MARKER= |
| 57 | +INPUT_FILE= |
| 58 | +INPUT_CONTENT= |
| 59 | +PRINT_LINE_NUMBER=false |
| 60 | +BEFORE_CONTEXT=0 |
| 61 | +AFTER_CONTEXT=0 |
| 62 | +declare -a GREP_CMD=(grep) |
| 63 | + |
| 64 | +# --- Commandline parsing --- |
| 65 | +if ! ARGS=$(getopt -o hanB:A:C: -l "help,version,line-number,before-context,after-context,context" -n "$SCRIPT" -- "$@"); then |
| 66 | + echo "$@" |
| 67 | + usage |
| 68 | + exit 2 |
| 69 | +fi |
| 70 | + |
| 71 | +eval set -- "$ARGS"; |
| 72 | + |
| 73 | +while true; do |
| 74 | + case $1 in |
| 75 | + -h|--help) |
| 76 | + shift; |
| 77 | + usage |
| 78 | + exit 0; |
| 79 | + ;; |
| 80 | + --version) |
| 81 | + shift; |
| 82 | + version; |
| 83 | + exit 0; |
| 84 | + ;; |
| 85 | + -a|--text) |
| 86 | + shift; |
| 87 | + GREP_CMD+=('--text') |
| 88 | + ;; |
| 89 | + -n|--line-number) |
| 90 | + shift; |
| 91 | + PRINT_LINE_NUMBER=true |
| 92 | + ;; |
| 93 | + -A|--after-context) |
| 94 | + AFTER_CONTEXT=$2; |
| 95 | + shift 2; |
| 96 | + ;; |
| 97 | + -B|--before-context) |
| 98 | + BEFORE_CONTEXT=$2; |
| 99 | + shift 2; |
| 100 | + ;; |
| 101 | + -C|--context) |
| 102 | + BEFORE_CONTEXT=$2; |
| 103 | + AFTER_CONTEXT=$2; |
| 104 | + shift 2; |
| 105 | + ;; |
| 106 | + --) |
| 107 | + shift; |
| 108 | + break; |
| 109 | + ;; |
| 110 | + esac |
| 111 | +done |
| 112 | + |
| 113 | +# --- Functions --- |
| 114 | +is_int() { |
| 115 | + if [ $1 -eq $1 ] 2>/dev/null; then |
| 116 | + return 0 |
| 117 | + else |
| 118 | + return 1 |
| 119 | + fi |
| 120 | +} |
| 121 | + |
| 122 | +# $1 start marker |
| 123 | +get_start_line() { |
| 124 | + local start_marker=$1 |
| 125 | + |
| 126 | + if is_int "${start_marker}"; then |
| 127 | + sline=${start_marker} |
| 128 | + elif [ "${start_marker}" != "-" ] ; then |
| 129 | + sline=$("${GREP_CMD[@]}" -m 1 -n "${start_marker}" <(echo "${INPUT_CONTENT}")|cut -d: -f1) || { echo "start marker '${start_marker}' not found in file" && exit 1; } |
| 130 | + else |
| 131 | + sline=1 |
| 132 | + fi |
| 133 | + echo "${sline}" |
| 134 | +} |
| 135 | + |
| 136 | +# $1 end marker |
| 137 | +# $2 start line |
| 138 | +get_end_line() { |
| 139 | + local end_marker="$1" |
| 140 | + local start_line="$2" |
| 141 | + |
| 142 | + if is_int "${end_marker}"; then |
| 143 | + eline=end_marker |
| 144 | + elif [ "${end_marker}" != "-" ] ; then |
| 145 | + eline=$("${GREP_CMD[@]}" -m 1 -n "${end_marker}" <(echo "${INPUT_CONTENT}"|tail -n+${start_line})|cut -d: -f1) || { echo "end marker '${end_marker}' not found in file" && exit 1; } |
| 146 | + ((eline+=${start_line}-1)) |
| 147 | + else |
| 148 | + eline=$(wc -l <(echo "${INPUT_CONTENT}")|cut -s -d " " -f1) |
| 149 | + fi |
| 150 | + echo "${eline}" |
| 151 | +} |
| 152 | + |
| 153 | +slice() { |
| 154 | + local start_marker="$1" |
| 155 | + local end_marker="$2" |
| 156 | + local sline |
| 157 | + local eline |
| 158 | + |
| 159 | + sline="$(get_start_line "${start_marker}")" |
| 160 | + eline="$(get_end_line "${end_marker}" "${sline}")" |
| 161 | + |
| 162 | + ((sline-=${BEFORE_CONTEXT})) |
| 163 | + ((eline++)) |
| 164 | + ((eline+=${AFTER_CONTEXT})) |
| 165 | + linediff="$((eline-sline))" |
| 166 | + if $PRINT_LINE_NUMBER; then |
| 167 | + cat -n <(echo "${INPUT_CONTENT}") | tail -n+${sline} |head -n${linediff} |
| 168 | + else |
| 169 | + tail -n+${sline} <(echo "${INPUT_CONTENT}")|head -n${linediff} |
| 170 | + fi |
| 171 | +} |
| 172 | + |
| 173 | +# --- Execution --- |
| 174 | + |
| 175 | +START_MARKER="$1" |
| 176 | +END_MARKER="$2" |
| 177 | +INPUT_FILE="${3:-}" |
| 178 | + |
| 179 | +if [ -n "${INPUT_FILE}" ] && [ ! -f "${INPUT_FILE}" ]; then |
| 180 | + echo "Input file '${INPUT_FILE}' does not exist." |
| 181 | + exit 4 |
| 182 | +fi |
| 183 | + |
| 184 | +if [ -n "${INPUT_FILE}" ]; then |
| 185 | + INPUT_CONTENT=$(<"${INPUT_FILE}") |
| 186 | +else |
| 187 | + INPUT_CONTENT=$(</dev/stdin) |
| 188 | +fi |
| 189 | + |
| 190 | + |
| 191 | +slice "${START_MARKER}" "${END_MARKER}" |
0 commit comments