string_out: remova a primeira ocorrência de uma string em um arquivo de texto
Publicado por Luís Fernando C. Cavalheiro (última atualização em 25/04/2016)
[ Hits: 2.663 ]
Homepage: https://github.com/lcavalheiro/
Cambada de clicadores, aqui começa mais um Script do Dino® trazendo para todos uma ferramenta útil ou curiosa. Hoje temos o string_out, que remove a primeira ocorrência de uma string em um arquivo de texto. Divirtam-se com ela!
#!/bin/bash
#
# ------------------------------- Legal notice -------------------------------
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Tradução não-oficial:
#
# Este programa é um software livre; você pode redistribuí-lo e/ou
# modificá-lo dentro dos termos da Licença Pública Geral GNU como
# publicada pela Fundação do Software Livre (FSF); na versão 3 da
# Licença, ou (na sua opinião) qualquer versão.
#
# Este programa é distribuído na esperança de que possa ser útil,
# mas SEM NENHUMA GARANTIA; sem uma garantia implícita de ADEQUAÇÃO
# a qualquer MERCADO ou APLICAÇÃO EM PARTICULAR. Veja a
# Licença Pública Geral GNU para maiores detalhes.
#
# Você deve ter recebido uma cópia da Licença Pública Geral GNU junto
# com este programa. Se não, veja <http://www.gnu.org/licenses/>.
#
# ------------------------------- Legal notice -------------------------------
#
# Define help and version funcionts
show_help() {
cat << EOF
Copyright 2016 Luís Fernando Carvalho Cavalheiro
Licence: GPLv3 (run "string_out -l" to read licence abstract)
string_out
description: removes first occurrence of string "string" from file "file"
usage: string_out [ -s "string" -f "file" -c "suffix" ] | [ -h | -v | -l ]
options:
-s "string" defines which string will be removed, quotes required
-f "file" defines from which file string will be removed, quotes required
-c "suffix" creates a copy of original file attaching "suffix" at its name
-h shows this help text
-v shows string_out version and changelog
-l shows GPLv3 abstract
If used to remove a string from file, options -s and -f are mandatory. If any of
-h, -v or -l options are used, they will take precedency over -s and -f and this
script will use only the last one passed to command line.
EOF
}
show_version() {
cat << EOF
Copyright 2016 Luís Fernando Carvalho Cavalheiro
Licence: GPLv3 (run "string_out -l" to read licence abstract)
string_out
description: removes first occurrence of string "string" from file "file"
string_out version and changelog history:
0.1 (RJU 20160331): first functional draft.
1.0 (RJU 20160331): first complete functional version
1.1 (RJU 20160331): added -c option
1.2 (RJU 20160331): simplified sed command, thanks to Elder Marco
EOF
}
show_licence() {
cat << EOF
Copyright(C) 2016 Luís Fernando Carvalho Cavalheiro
Licenced under GPLv3.
string_out
description: removes first occurrence of string "string" from file "file"
Generic Public Licence, version 3, abstract:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
EOF
}
set -e
# Main script loop, eval command line arguments
while getopts ":hvls:f:c:" opt ; do
case "$opt" in
s)
ACTION="s"
STRING="$OPTARG"
;;
f)
CHECKACTION="s"
FILE="$OPTARG"
;;
c)
SUFFIX="$OPTARG"
;;
h)
ACTION="h"
;;
v)
ACTION="v"
;;
l)
ACTION="l"
;;
:)
echo "Option -$OPTARG requires an argument." >&2
show_help
exit 1
;;
\?)
echo "Invalid option: -$OPTARG." >&2
show_help
exit 1
;;
esac
done
# Shows help text if no option was provided on command line
if [ $OPTIND -eq 1 ] ; then
cat << EOF
string_out requires at least one argument!
EOF
ACTION="h"
fi
# If processing a file, check if both -s and -f options were provided
if [ "$ACTION" != "$CHECKACTION" ] ; then
cat << EOF
You need to provide both -s and -f options with their respective arguments!
EOF
ACTION="h"
fi
# Process command line options
case "$ACTION" in
h)
show_help
;;
v)
show_version
;;
l)
show_licence
;;
s)
sed -i"$SUFFIX" -e "0,/$STRING/ { /$STRING/ d }" "$FILE"
;;
esac
Carrega o VNC em uma tela do X11 separada e com mouse para canhotos
Configurando seus arquivos no Slackware
Script "go" v.2 - Inicia Sessoes SSH e Terminal Service
mysqlapache2combo - Pequeno script que facilita a vida
Nenhum comentário foi encontrado.
Monitorando o Preço do Bitcoin ou sua Cripto Favorita em Tempo Real com um Widget Flutuante
IA Turbina o Desktop Linux enquanto distros renovam forças
Como extrair chaves TOTP 2FA a partir de QRCODE (Google Authenticator)
Como realizar um ataque de força bruta para desobrir senhas?
Como usar Gpaste no ambiente Cinnamon
Atualizando o Fedora 42 para 43
Scripts ou binários [RESOLVIDO] (3)
VOL já não é mais como antes? (10)
Pergunta: Meu teclado não está respondendo direito como e consertar? ... (4)









