|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2018 Juliano Santos [SHAMAN] |
| 4 | +# |
| 5 | +# This file is part of bashsrc. |
| 6 | +# |
| 7 | +# bashsrc is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU General Public License as published by |
| 9 | +# the Free Software Foundation, either version 3 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# bashsrc is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU General Public License |
| 18 | +# along with bashsrc. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +[[ $__IO_SH ]] && return 0 |
| 21 | + |
| 22 | +readonly __IO_SH=1 |
| 23 | + |
| 24 | +source builtin.sh |
| 25 | + |
| 26 | +__TYPE__[io_t]=' |
| 27 | +io.rmline |
| 28 | +io.rmlinecontains |
| 29 | +io.rmlinematch |
| 30 | +' |
| 31 | + |
| 32 | +# func io.rmline <[file]filename> <[bool]writer> <[bool]invert> <[uint]number> ... => [str] |
| 33 | +# |
| 34 | +# Remove linhas especificas. |
| 35 | +# |
| 36 | +# filename - Caminho completo do arquivo. |
| 37 | +# writer - Gravar alterações no arquivo. |
| 38 | +# invert - Inverter a seleção especificada. |
| 39 | +# number - Número da linha a ser removida. |
| 40 | +# |
| 41 | +# Obs: Pode ser especificada uma ou mais linhas. |
| 42 | +# |
| 43 | +function io.rmline() |
| 44 | +{ |
| 45 | + getopt.parse -1 "filename:file:+:$1" "writer:bool:+:$2" "invert:bool:+:$3" "line:uint:+:$4" ... "${@:4}" |
| 46 | + |
| 47 | + local ln lns line tmp |
| 48 | + |
| 49 | + tmp=$(mktemp -qtu XXXXXXXXXXXXXXXXXXXX.io) |
| 50 | + lns=${@:4} |
| 51 | + lns=${lns// /|} |
| 52 | + |
| 53 | + while read line; do |
| 54 | + if [[ $3 == false ]]; then |
| 55 | + [[ $((++ln)) == @($lns) ]] |
| 56 | + else |
| 57 | + [[ $((++ln)) != @($lns) ]] |
| 58 | + fi && continue |
| 59 | + echo "$line" |
| 60 | + done < "$1" >> $tmp |
| 61 | + |
| 62 | + [[ $2 == false ]] && cat $tmp || io.__write_file "$tmp" "$1" |
| 63 | + |
| 64 | + rm -f "$tmp" |
| 65 | + |
| 66 | + return $? |
| 67 | +} |
| 68 | + |
| 69 | +# func io.rmlinecontains <[file]filename> <[bool]writer> <[bool]invert> <[str]pattern> ... => [str] |
| 70 | +# |
| 71 | +# Remove a(s) linha(s) que contém a expressão. |
| 72 | +# |
| 73 | +# filename - Caminho completo do arquivo. |
| 74 | +# writer - Gravar alterações no arquivo. |
| 75 | +# invert - Inverter a seleção especificada. |
| 76 | +# pattern - Sequência de caracateres que determina o padrão a casar. |
| 77 | +# |
| 78 | +# Obs: Pode ser especificado mais de um padrão. |
| 79 | +# |
| 80 | +function io.rmlinecontains() |
| 81 | +{ |
| 82 | + getopt.parse -1 "filename:file:+:$1" "writer:bool:+:$2" "invert:bool:+:$3" "pattern:str:+:$4" ... "${@:5}" |
| 83 | + |
| 84 | + local line tmp patterns |
| 85 | + |
| 86 | + tmp=$(mktemp -qtu XXXXXXXXXXXXXXXXXXXX.io) |
| 87 | + patterns=${@:4} |
| 88 | + patterns=${patterns// /|} |
| 89 | + |
| 90 | + while read line; do |
| 91 | + if [[ $3 == false ]]; then |
| 92 | + [[ $line == *@($patterns)* ]] |
| 93 | + else |
| 94 | + [[ $line != *@($patterns)* ]] |
| 95 | + fi && continue |
| 96 | + echo "$line" |
| 97 | + done < "$1" >> $tmp |
| 98 | + |
| 99 | + [[ $2 == false ]] && cat $tmp || io.__write_file "$tmp" "$1" |
| 100 | + |
| 101 | + rm -f "$tmp" |
| 102 | + |
| 103 | + return $? |
| 104 | +} |
| 105 | + |
| 106 | +# func io.rmlinematch <[file]filename> <[bool]writer> <[bool]invert> <[str]pattern> ... => [str] |
| 107 | +# |
| 108 | +# Remove a(s) linha(s) que conicide(m) com o padrão especificado. |
| 109 | +# |
| 110 | +# filename - Caminho completo do arquivo. |
| 111 | +# writer - Gravar alterações no arquivo. |
| 112 | +# invert - Inverter a seleção especificada. |
| 113 | +# pattern - Sequência de caracateres que determina o padrão a casar. |
| 114 | +# Suporta ERE (Expressão Regular Estendida). |
| 115 | +# |
| 116 | +# Obs: Pode ser especificado mais de um padrão. |
| 117 | +# |
| 118 | +function io.rmlinematch() |
| 119 | +{ |
| 120 | + getopt.parse -1 "filename:file:+:$1" "writer:bool:+:$2" "invert:bool:+:$3" "pattern:str:+:$4" ... "${@:5}" |
| 121 | + |
| 122 | + local line tmp patterns |
| 123 | + |
| 124 | + tmp=$(mktemp -qtu XXXXXXXXXXXXXXXXXXXX.io) |
| 125 | + patterns=${@:4} |
| 126 | + patterns=${patterns// /|} |
| 127 | + |
| 128 | + while read line; do |
| 129 | + if [[ $3 == false ]]; then |
| 130 | + [[ $line =~ $patterns ]] |
| 131 | + else |
| 132 | + [[ ! $line =~ $patterns ]] |
| 133 | + fi && continue |
| 134 | + echo "$line" |
| 135 | + done < "$1" >> $tmp |
| 136 | + |
| 137 | + [[ $2 == false ]] && cat $tmp || io.__write_file "$tmp" "$1" |
| 138 | + |
| 139 | + rm -f "$tmp" |
| 140 | + |
| 141 | + return $? |
| 142 | +} |
| 143 | + |
| 144 | + |
| 145 | +function io.__write_file() |
| 146 | +{ |
| 147 | + if ! echo "$(< "$1")" > "$2" &>/dev/null; then |
| 148 | + error.format 1 "falha ao gravar as alterações no arquivo '%s'" "$2" |
| 149 | + return $? |
| 150 | + fi |
| 151 | + |
| 152 | + return $? |
| 153 | +} |
| 154 | +source.__INIT__ |
| 155 | +# /* __IO_SH */ |
0 commit comments